2

我正在尝试开发一个基本的聊天应用程序。我已经为服务器和客户端编写了所需的代码,并且该应用程序在 LAN 上运行良好。然而,当我试图让它在互联网上运行时,我被卡住了。我了解到我需要在服务器机器中进行端口转发,并且我做到了。我转发了 6000 端口,canyouseeme.org 给出了成功的消息。因此,端口肯定是打开的,并且没有防火墙阻止它。但是,客户端代码无法连接到它。

服务器代码按如下方式打开一个 ServerSocket(它们的定义在前面已经做了):

InetAddress IP = InetAddress.getLocalHost();
welcomeSocket = new ServerSocket(6000, 10, IP);
connectionSocket = welcomeSocket.accept();

客户端代码主要使用以下代码尝试连接到它:

clientSocket = new Socket("xx.xxx.xxx.xx", 6000);

其中 xx.xxx.xxx.xx 是服务器机器的 IP 地址(不是本地的 192.168.xx)。

这是Java问题吗?我问是因为端口 6000 显然是打开的,并且 canyouseeme.org 可以成功连接到它,但我的客户端无法连接。为什么会发生这样的事情?任何帮助将不胜感激(有关更多信息,请阅读下面的评论)。

错误信息如下:

java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at java.net.Socket.<init>(Socket.java:366)
    at java.net.Socket.<init>(Socket.java:180)
    at ChatPanel$ConnectButtonListener.actionPerformed(ChatPanel.java:206)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6038)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.awt.Component.processEvent(Component.java:5803)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.java:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
4

1 回答 1

1

在客户端上,您应该使用

clientSocket = new Socket(InetAddress.getByAddress(addr), port);

whereaddr是服务器的公共地址,并且port是绑定到的服务器。

于 2012-08-29T11:04:58.440 回答