我得到了以下代码:
public static readonly IPEndPoint RootNode = new IPEndPoint(IPAddress.Parse("213.226.18.82"), 8333);
public static void Main(string[] args)
{
TcpClient tcpClient = new TcpClient(RootNode);
}
这将抛出一个 SocketException
消息 The requested address is not valid in its context
。
现在奇怪的是这段代码有效:
public static readonly IPEndPoint RootNode = new IPEndPoint(IPAddress.Parse("213.226.18.82"), 8333);
public static void Main(string[] args)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(RootNode);
}
这里有什么区别?
最佳答案
在 https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.tcpclient%28v=vs.110%29.aspx 的文档中它谈到了这个构造函数:
TcpClient(IPEndPoint): Initializes a new instance of the TcpClient class and binds it to the specified local endpoint.
所以在这里它说它将把它绑定(bind)到本地地址,就像你正在监听某个端口一样。 Connect()
命令会将您连接到一个远程 端点。这应该是不同之处。
关于c# - TcpClient 抛出 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33023427/