C# 中的网络,而不是局域网
我想了解 C# 中的网络是如何工作的,所以我学习了如何使用 TCP 服务器和客户端。 唯一的问题是,只有当两台计算机连接到同一网络时它才能工作。
即使它们没有连接到同一网络,我怎样才能使它们进行通信?
I wanted to learn how networking in c# works, so I learned how to use TCP server and clients.
The only problem is that it's working only if both computers are connected to the same network..
How can I make them communicate even if they aren't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TCP/IP 套接字应该在任意两个端点之间工作,只要它们之间存在路由即可。如果它们之间没有路由,那么您正在讨论有两个独立的断开连接的网络的情况。在这种情况下,您将需要一些东西来桥接两个网络。
如果您使用 TCP/IP 服务器/客户端通信,并且计算机位于不同的网络上,并且有连接它们的路由,并且它们无法通信,那么您应该查看防火墙设置和其他网络设置,以确保来自一个网络的 TCP/IP 数据包能够到达其他网络。
当客户端尝试连接到服务器时,请确保您使用正确的 IP 地址。如果您有一台 IP 地址为 10.0.0.5 的服务器正在侦听端口 4823,请尝试使用服务器 IP 地址 10.0.0.5 和端口 4823 从客户端远程登录到该 IP 地址。如果可以连接,通常意味着您的设置正确。
从命令提示符处:
telnet 10.0.0.5 4823
TCP/IP sockets should work between any two end points as long as there is a route between them. If there is no route between them then you are talking about a case where there are two separate disconnected networks. In that case you will need something to bridge the two networks.
If you are using TCP/IP server/client communication and the computers are on different networks that has a route connecting them and they cannot communicate then you should look at firewall settings and other network settings to make sure TCP/IP packets from one network are able to reach the other network.
Make sure you are using the correct IP address when the client tries to connect to the server. If you have a server at IP address 10.0.0.5 listening on port 4823 try to telnet to that IP address from the client using the server IP address 10.0.0.5 and port 4823. If it connects that usually means that you have things set up right.
From a command prompt:
telnet 10.0.0.5 4823
TCP 中的通信是通过 IP 地址完成的。因此,即使客户端和服务器不在同一网络上,如果您指定服务器的 IP 地址,客户端也将能够与其通信(当然假设客户端所在的网络配置正确并且知道如何进行通信)到达服务器的网络)。您还可以使用 DNS 服务并提供服务器的 FQDN 而不是 IP 地址。客户端网络上的 DNS 服务器会将服务器的 FQDN 解析为 IP 地址。
Communication in TCP is done with IP addresses. So even if the client and the server are not on the same network if you specify the IP address of the server, the client will be able to communicate with it (assuming of course the network that the client resides on is configured properly and knows how to reach the server's network). You could also use the DNS service and provide the FQDN of the server instead of an IP address. The DNS server on the client network will resolve the server's FQDN to an IP address.