我正在尝试开发一个基本的聊天应用程序。我已经为服务器和客户端编写了所需的代码,应用程序在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.x.x的本地IP地址)。
这是一个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)
答案 0 :(得分:1)
在客户端上,您应该使用
clientSocket = new Socket(InetAddress.getByAddress(addr), port);
其中addr
是服务器的公共地址,port
是绑定的服务器。