开发者

Is there a way to specify the local port to used in tcpClient?

开发者 https://www.devze.com 2022-12-31 14:20 出处:网络
I am currently using this function call to create my tcpClient: clientSocket = new TcpClient(\"localhost\", clientPort);

I am currently using this function call to create my tcpClient:

clientSocket = new TcpClient("localhost", clientPort);

But the clientPort is the server's port.

Is there a way for me to specify the client开发者_C百科 port using tcpClient?

Thanks


The constructor overload that takes an IPEndPoint allows you to bind the internal Socket of the TcpClient to a specific port:

IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, clientPort);
TcpClient clientSocket = new TcpClient(ipLocalEndPoint);
clientSocket.Connect(remoteHost, remotePort);
0

精彩评论

暂无评论...
验证码 换一张
取 消