简体   繁体   English

C# TcpListener 和 TcpClient 在一个网络中的不同 PC 上

[英]C# TcpListener and TcpClient on separate PCs in one network

I am trying to understand how Tcp communication is done in C#.我试图了解 Tcp 通信是如何在 C# 中完成的。 I have a simple program, which works fine if the client and listener are on one PC (using localhost).我有一个简单的程序,如果客户端和侦听器在一台 PC 上(使用 localhost),它可以正常工作。

Then I tried starting the two programs from separate PCs (in the same WiFi network) changing localhost to the local IP address (192.168.0.x) for both programs.然后我尝试从不同的 PC(在同一个 WiFi 网络中)启动这两个程序,将两个程序的 localhost 更改为本地 IP 地址(192.168.0.x)。 But that does not work (I get an exception that the address is not known).但这不起作用(我得到一个地址未知的例外)。

I suppose I am missing something very basic since I do not have a good knowledge on networks.我想我缺少一些非常基本的东西,因为我对网络没有很好的了解。 Is there a simple way to solve this?有没有简单的方法来解决这个问题? If not, do you have any hints where I could start studying?如果没有,你有什么建议我可以开始学习吗?

I have been looking quite a lot but all examples you find are with localhost.我一直在寻找很多,但您找到的所有示例都与 localhost 相关。

Thanks for the help.谢谢您的帮助。

For the PC that runs the listener you have to allow incoming connections on whatever port you picked (presumably in Windows Firewall) and also put the IP the client uses to connect to it in the TCPListener constructor.对于运行侦听器的 PC,您必须允许您选择的任何端口上的传入连接(大概在 Windows 防火墙中),并将客户端用来连接它的 IP 放在 TCPListener 构造函数中。 Then the server will only accept connections from that network interface that has the IP specified.然后服务器将只接受来自指定了 IP 的网络接口的连接。 On the client you just put the same IP and Port.在客户端上,您只需放置相同的 IP 和端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM