How do I get an IP address from a socket?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a server to open a TCPListener, the client connects, and the
server returns a Socket. I want to know what IP address is connected to my
server. How do I retrieve the IP address of my client from the Socket?

Thanks.
 
I am answering my own question.

Socket socketForClient =
tcpListener.AcceptSocket( );

if (socketForClient.Connected)
{
Console.WriteLine("{0}", socketForClient.RemoteEndPoint.ToString( ));
Console.WriteLine("Client connected");
}

That will print the IP address and port of the client connected on
socketForClient.
 
Back
Top