在 C# 中设置 TcpClient 本地端点时出现问题

发布于 2024-08-21 12:57:56 字数 293 浏览 7 评论 0原文

为什么以下代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

固执像三岁 2024-08-28 12:57:56

两个 TcpClient 无法相互通信。您需要一个 TcpClient 和一个 TcpListener

Two TcpClient's can't talk to each other. You need one TcpClient and one TcpListener.

独自←快乐 2024-08-28 12:57:56

问题是您没有监听连接。您必须使用 TcpListener 或类似的。

The problem is that you're not listening for connections. You have to use a TcpListener or similar.

凉世弥音 2024-08-28 12:57:56

我同意萨姆的观点。您可以在此处找到示例。

I agree with Sam. You can find an example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文