I have a socket server to catch HTTP request from Client. I want to get Client’s Ip so I used these code :
var sIp = (socket.RemoteEndPoint.ToString().Split(':'))[0];
IPAddress rIp = IPAddress.Parse(sIp);
string clientIp = rIp.ToString();
But the Ip returned was wrong and there was an Exception : ‘ ‘rIp’ threw an exception of type ‘System.Net.Sockets.SocketException’
Do anyone have solution for this case ? Tks so much.
MSDN has this information on socket.RemoteEndpoint: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.remoteendpoint.aspx
The key part is “You must cast this EndPoint to an IPEndPoint before retrieving any information.”
So the code could be re-written as:
however the rIp isn’t needed you could just do: