How do I get client ip address using TcpClient?

Posted by brendan on Stack Overflow See other posts from Stack Overflow or by brendan
Published on 2010-04-26T22:29:26Z Indexed on 2010/04/26 22:33 UTC
Read the original article Hit count: 250

Filed under:
|
|

I am using TcpClient to listen on a port for requests. When the requests come in from the client I want to know the client ip making the request.

I've tried:

Console.WriteLine(tcpClient.Client.RemoteEndPoint.ToString());
Console.WriteLine(tcpClient.Client.LocalEndPoint.ToString());
var networkStream = tcpClient.GetStream();
var pi = networkStream.GetType().GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
var socketIp = ((Socket)pi.GetValue(networkStream, null)).RemoteEndPoint.ToString();
Console.WriteLine(socketIp);

All of these addresses output 10.x.x.x addresses which are private addresses and are clearly not the address of the clients off my network making the requests. What can I do to get the public ip of the clients making the requests?

© Stack Overflow or respective owner

Related posts about c#

Related posts about tcpclient