JuiCe
JuiCe

Reputation: 4191

Socket property that returns IP address/Port

I am trying to get a String representation of the IP Address and Port that my Socket has been initialized with. I am using this data to print in a message box for the user if an error occurs.

Using the WinSock control in VB6 the following code was used:

"Could not open TCP Connection to " & frmMain.winSock1.RemoteHost & ":" 
          & frmMain.winSock1.RemotePort

Any help at all would be appreciated.

Upvotes: 1

Views: 783

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595742

The Socket does not know the remote IP/Port until it actually connects to the server. You have to provide a destination to its Connect() method first. If Connect() fails, you have to know the destination you provided so you can report it, you cannot query the Socket for it. If you connect asynchronously, you will have to remember that destination somewhere so you can retrieve it when needed.

However, if Connect() succeeds, you can query the RemoteEndPoint property to get the actual IP/Port that the Socket connected to. This is particularly useful when connecting to a hostname, to discover what IP address the hostname resolved to.

Upvotes: 4

Related Questions