本文介绍了Internet上的Java套接字超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小型聊天程序,该程序在客户端&服务器在同一台计算机(也可能是网络)上运行.但是,一旦我尝试通过Internet连接到另一台计算机,套接字连接就会超时.这是由于防火墙/路由器等引起的吗?

I created a small chat program, that works flawlessly when client & server are run on the same computer (and probably network, too).However, as soon as I try to connect to another computer over the internet, the socket connection simply times out.Is this because of firewalls / routers, etc?

以及如何连接ServerSocket& ;?通过互联网套接字吗?

And how can I connect a ServerSocket & Socket over the internet?

推荐答案

是的,很有可能.您遇到了 NAT 问题:本质上是一样的外部可见的IP地址映射到许多内部可见的终结点,并且外部终结点不知道向您的套接字请求发送到哪个内部终结点.

Yes, most likely. You're running into the NAT problem: essentially, the same externally visible IP address maps to many internally visible endpoints, and external endpoint doesn't know which internal endpoint to give your socket request to.

最简单的方法是让两个客户都连接到他们都能看到的第三方,然后让第三方来协调通信.例如,大多数即时通讯协议就是这样工作的.

The easiest way around this is to have both your clients connect to a third party which both of them can see, and then have the third party mediate the communication. This is how most instant-messaging protocols work, for example.

如果您无法控制这样的第三方实体,则直接连接两个客户端的另一种方法是让两个客户端都打开一个商定的端口,然后将该端口上的通信映射到他们自己的内部端点.这提供了外部可见的终结点(例如您的家庭路由器)将通信传递到其预期目标所需的缺失链接.

If you have no way to control a third-party entity like that, an alternative to directly connect two clients is to have both clients open up an agreed-upon port, and then map communications on that port to their own internal endpoint. This provides the missing link that the externally visible endpoint (e.g. your home router) needs to deliver the communication to its intended destination.

这篇关于Internet上的Java套接字超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:06