1

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.

JuiCe
  • 4,132
  • 16
  • 68
  • 119
  • 1
    You would be better to show the VB.NET code instead of the VB6 so we can see how it is being initalised etc. – Matt Wilko Jun 18 '14 at 15:24
  • @MattWilko I believe OP is just showing what worked in VB6 in the past, and is looking for an approximation to that. – djv Jun 18 '14 at 16:21
  • This question has more or less been asked before: http://stackoverflow.com/questions/1904160/getting-the-ip-address-of-a-remote-socket-endpoint – djv Jun 18 '14 at 16:22
  • @Verdolino the link you provided is for C#, not vb.net. – Cary Bondoc Aug 13 '15 at 08:13
  • It's really a simple question about System.Net.Sockets.Socket. As seen in the accepted answer, it is a .Net language agnostic question. – djv Aug 13 '15 at 12:26

1 Answers1

4

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770