Get remote port of a connected TcpClient

16,570
var port = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port

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

HTH.

Share:
16,570
kmarks2
Author by

kmarks2

Updated on June 29, 2022

Comments

  • kmarks2
    kmarks2 almost 2 years

    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.