Socket Error

J

Joey Wu

Dear All

I wrote this function to try to connect 2 devices.
The error message is "inValidCastException".
Please help and thanks a lot!

Public Function Connect(ByVal ServerIP As String, ByVal ServerPort
As Integer) As Boolean

Try
SocClient = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.IP)
Dim ep As System.Net.IPEndPoint = New
System.Net.IPEndPoint(IPAddress.Loopback, 16999)
SocClient.Bind(ep)
Dim rEP As System.Net.IPEndPoint = New
System.Net.IPEndPoint(ServerIP, ServerPort)
SocClient.Connect(rEP)
Catch ex As Exception
MsgBox(ex.ToString)
End Try

Return SocClient.Connected

End Function
 
G

Guest

I don't know what's the problem but if you need to communicate through IP,
then you'd find TcpClient and TcpListener rather easier than Sockets. You
create them in the same way but require less fuss I think.
 
J

Joey Wu

This line.

Dim rEP As System.Net.IPEndPoint = New System.Net.IPEndPoint(ServerIP,
ServerPort)

The ServerIP is "192.168.1.132"
ServerPort is 16888

Thanks!!
 
P

Paul G. Tobey [eMVP]

And ServerIP is an instance of what? Look at the declaration of the
IPEndPoint constructor. You'll note that there is no overload which takes a
string and an integer. You want to pass it an IPAddress and an integer.
You need to convert your string IP address to an IPAddress instance using
the Parse static method.

Paul T.

Joey Wu said:
This line.

Dim rEP As System.Net.IPEndPoint = New System.Net.IPEndPoint(ServerIP,
ServerPort)

The ServerIP is "192.168.1.132"
ServerPort is 16888

Thanks!!

"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com>
¦b
¶l¥ó news:[email protected] ¤¤¼¶¼g...
On which line of code do you get the error?!?!

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top