Java Gateway Connection / WAN Servers

I am trying to get the server out of my computer so that clients from their computers can connect and communicate with my computer. I made a server on port 31350, and the client is trying to connect through my router IP. But it only works through lan when I have "localhost" or the name of my computer in the options for creating a socket. not when I use my IP address, starting the client and server on different networks. Here is the code.

Here is the server my computer is running on.

public static void main (String[] args) throws IOException { ServerSocket server = new ServerSocket(31350); Socket client1 = server.accept(); } 

Here is the client code that my friend runs on his computer.

 public static void main(String[] args) throws IOException, UnknownHostException { Socket socket; // # are what I got from whatismyip.org on the server computer) byte[] serverb = new byte[] {(byte)##, (byte)##, (byte)###, (byte)###}; socket = new Socket(InetAddress.getByAddress(serverb),31350); } 

This is what he says when I launch the client

Exception in the main thread java.net.ConnectException: connection timeout: connect on java.net.DualStackPlainSocketImpl.connect0 (native method) on java.net.DualStackPlainSocketImpl.socketConnect (DualStackPlainSocketImpl.java:69) on java.net.AbstractPlainSocket .doConnect (AbstractPlainSocketImpl.javahaps39) on java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:200) on java.net.AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:182) on java.net.Plain.plainplpl.plain.plainconnplpl.splainplpl.plainplpl.plainconnplplpl java: 157) on java.net.SocksSocketImpl.connect (SocksSocketImpl.javahaps91) on java.net.Socket.connect (Socket.java∗79) on java.net.Socket.connect (Socket.java► java.net.Socket. (Socket.java-00-0025) in java.net.Socket. (Socket.java:241) in ClientTest.main (ClientTest.java:22) // 22 is the line socket = new Socket (InetAddress.getByAddress (serverb), 31350);

Firewalls

disconnected. port 31350 on my router is redirected to the local IP address of my computer, which I received from using ipconfig in cmd. But it still does not work, I just get an IOException when trying to create a socket from the client computer. Nothing happens on the server computer. What's wrong?

+4
source share
1 answer

[update]

As you would expect, a connection timeout indicates some network problem. Packets from your client do not arrive on the server machine. The exact solution will depend on the type of router, but the term google for is "port forwarding." Here is an article I randomly found to help: http://www.rhinosoft.com/KnowledgeBase/kbarticle.asp?RefNo=1289

Basically, you program the router so that any connection request on port 31350 is redirected to your computer on the LAN at the local IP address.

Good luck

[original comment]

This is more of a comment than an answer (but I need an extra room). The logic of your logic is trying to significantly complicate the diagnosis of the problem. Simplify the code as follows:

 public static void main(String[] args) throws IOException, UnknownHostException { Socket socket; // # are what I got from whatismyip.org on the server computer) byte[] serverb = new byte[] {(byte)##, (byte)##, (byte)###, (byte)###}; socket = new Socket(InetAddress.getByAddress(serverb),31350); } 

Just let the original IOException propagate and update your question to enable traceability of the exception stack. The original exception contains valuable information - if it says that the connection was refused, it means one thing: your port number may be incorrect. If he says that the connection timeout means something else - either you really have a problem with the firewall, or maybe your IP address is incorrect.

Your code catches a useful exception, swallows it, and throws a much less useful exception.

Do the same with the server code:

 public static void main (String[] args) throws IOException { ServerSocket server = new ServerSocket(31350); Socket client1 = client1 = server.accept(); } 

The stack trace will show which method threw the exception, so you don’t need redundant text like InetAddress creation failed

+3
source

Source: https://habr.com/ru/post/1445164/


All Articles