如何通过IP地址连接电脑
这与c# Sockets有关。 我正在开发一个套接字程序。但我的软件有一个问题。 我无法连接到通过本地网络连接到互联网的计算机。但我可以连接到仅使用互联网的计算机。更具描述性, 考虑 5 台计算机通过使用 1 个 IP 地址的调制解调器连接互联网。当我尝试连接其中一台计算机时,API 通过其 IP 地址连接到调制解调器。但没有任何回应。因为调制解调器不响应计算机请求。顺便说一句,我的 API 必须到达计算机而不仅仅是调制解调器。并且抛出 SocketException。对于这个问题我能做什么?
This is related to c# Sockets.
I'm developping a socket program. But there is one problem in my software.
I cannot connect to computers which are connected to internet via theirs local network. But I can connect to computers that are uses internet alone. Be more descriptive,
Consider that 5 computers connects internet via a modem that uses 1 ip address. When I try to reach one of these computers, API connects to modem via its ip address. But there is no response. Because a modem don't respond a computer request. By the mean, my API must reach a computer not only modem. And and an SocketException is thrown. What can I do about this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到的问题是由 NAT 引起的。这是由允许多个客户端通过一个公共 IP 地址上网的路由器使用的。路由器后面的客户端都不会“知道”或“看到”这一点,但它限制了连接。
客户端可以发起与外部的连接,但反过来基本上是不可能的。除非您使用端口转发,其中一个或多个端口将转发到路由器后面的客户端。这样,就可以从外部发起连接。
然而,这需要在客户端进行配置,因此首选方法是让客户端连接到服务器,因为这始终是可能的(忽略防火墙)。
*:还有一种解决方法,您让一个客户端连接到您的服务器,然后将连接信息传递给另一个客户端,以便这些客户端可以相互通信。这称为“nat 打孔”,例如由 torrent 客户端使用。
The problem you're encountering is caused by NAT. This is used by routers who allow multiple clients to go online through one public IP address. None of the clients behind the router will 'know' or 'see' this, but it limits connectivity.
Clients can initiate a connection to the outside, but the other way around is basically* impossible. Unless you use port forwarding, where one or more ports are forwarded to a client behind the router. This way, connections can be initiated from the outside.
This requires configuration on the client side however, so the preferred way would be to let your clients connect to your server, since that will always be possible (firewalls neglected).
*: There also is a workaround, where you let a client connect to your server, and then pass the connection information to another client, so those clients can communicate with each other. This is called 'nat punching' and is used by torrent clients, for example.
你可以尝试这样的事情......
服务器端代码
客户端代码:
我希望它能帮助你
you can try something like this.....
server Side Code
Client side code :
I hope it will helps you