how to get client IP using socket programming c#
Is there anyone who knows how to 开发者_开发知识库get client IP address using socket programming while we are requesting the access of file transferring?? I am using C#.
In order to get the actual IP address:
// Using the RemoteEndPoint property.
Console.WriteLine (
"I am connected to " + IPAddress.Parse (((IPEndPoint)s.RemoteEndPoint).Address.ToString ()) +
"on port number " + ((IPEndPoint)s.RemoteEndPoint).Port.ToString ());
// Using the LocalEndPoint property.
Console.WriteLine (
"My local IpAddress is :" + IPAddress.Parse (((IPEndPoint)s.LocalEndPoint).Address.ToString ()) +
"I am connected on port number " + ((IPEndPoint)s.LocalEndPoint).Port.ToString ());
taken from the msdn site:
Socket.LocalEndPoint
or Socket.RemoteEndPoint
should do the trick, depending on whether you're the client or not.
Assuming you have a TcpListener
, after the AcceptSocket
call you are returned a Socket
. On this socket you can call RemoteEndPoint
精彩评论