I'm trying to develop a basic chat application. I've written the required codes for both the server and the client, and the application works perfectly fine over the LAN. When I tried to make it work over the Internet, however, I got stuck. I learned that I need to make a port-forwarding in the server machine, and I did that. I forwarded the port 6000 and canyouseeme.org gave a successful message. So, the port is surely open and no firewall is blocking it. However, the client code cannot connect to it.
The server code opens a ServerSocket as follows (their definitions are made previously):
InetAddress IP = InetAddress.getLocalHost();
welcomeSocket = new ServerSocket(6000, 10, IP);
connectionSocket = welcomeSocket.accept();
And the client code tries to connect to it mainly using the following code:
clientSocket = new Socket("xx.xxx.xxx.xx", 6000);
where xx.xxx.xxx.xx is the server machine's IP address (not the local one with 192.168.x.x).
Is this a Java issue? I am asking because the port 6000 is apparently open and canyouseeme.org can successfully connect to it, but my client fails to do so. Why such a thing happens? Any help would be appreciated (For further info, please read the comments below).
The error message is below:
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)
On the client you should use
clientSocket = new Socket(InetAddress.getByAddress(addr), port);
where addr
is the public address of the server and port
is the server bound to.