在 C# 中设置 TcpClient 本地端点时出现问题
为什么以下代码在 C# 中不起作用?
var c1 = new TcpClient(new IPEndPoint(IPAddress.Any, 8787));
var c2 = new TcpClient(new IPEndPoint(IPAddress.Any, 8788));
c1.Connect("localhost", 8788);
我收到“无法建立连接,因为目标计算机主动拒绝”。因此,TcpClient 构造函数似乎没有绑定端口,但我尝试了 Socket.Bind() 函数,但也没有成功。
Why won't the following code work in C#?
var c1 = new TcpClient(new IPEndPoint(IPAddress.Any, 8787));
var c2 = new TcpClient(new IPEndPoint(IPAddress.Any, 8788));
c1.Connect("localhost", 8788);
I get a "connection cannot be made because the target machine actively refused it". So, the TcpClient constructor doesn't appear to be binding the port, but I tried the Socket.Bind() function with no luck either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个
TcpClient
无法相互通信。您需要一个TcpClient
和一个TcpListener
。Two
TcpClient
's can't talk to each other. You need oneTcpClient
and oneTcpListener
.问题是您没有监听连接。您必须使用 TcpListener 或类似的。
The problem is that you're not listening for connections. You have to use a TcpListener or similar.
我同意萨姆的观点。您可以在此处找到示例。
I agree with Sam. You can find an example here.