Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get remote port of a connected TcpClient

For the purposes of constructing a rate limiter I need to be able to distinguish different users being routed through the same gateway. So if I have 100 clients with the same IP address I would like to tell them apart by what port they are associated with on the remote gateway/host/router.

I can get the IP easily with:

string clientIPAddress = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();

Is there some way similarly to get the remote port number for the client? Thanks.

like image 910
kmarks2 Avatar asked Nov 29 '22 02:11

kmarks2


1 Answers

var port = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port

Ref.: http://msdn.microsoft.com/en-us/library/system.net.ipendpoint.port.aspx

HTH.

like image 66
Sunny Avatar answered Dec 06 '22 20:12

Sunny