简体   繁体   中英

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#.

Socket.LocalEndPointSocket.RemoteEndPoint应该可以解决问题,具体取决于您是否是客户端。

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:

Assuming you have a TcpListener , after the AcceptSocket call you are returned a Socket . On this socket you can call RemoteEndPoint

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM