有没有办法指定在 tcpClient 中使用的本地端口? [英] Is there a way to specify the local port to used in tcpClient?
本文介绍了有没有办法指定在 tcpClient 中使用的本地端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用这个函数调用来创建我的 tcpClient:
I am currently using this function call to create my tcpClient:
clientSocket = new TcpClient("localhost", clientPort);
但是 clientPort
是服务器的端口.
But the clientPort
is the server's port.
有没有办法让我使用 tcpClient 指定客户端端口?
Is there a way for me to specify the client port using tcpClient?
谢谢
推荐答案
采用 IPEndPoint 的构造函数重载 允许您将 TcpClient 的内部 Socket 绑定到特定端口:
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);
这篇关于有没有办法指定在 tcpClient 中使用的本地端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文