Java Socket/Serversocket WAN Connection

对着背影说爱祢 提交于 2019-12-05 17:04:46

[update]

As you might expect, connection timed out indicates it's some kind of a network problem. The packets from your client are not arriving at the server machine. The exact solution will depend on the type of router, but the term to google for is "port forwarding". Here is an article I found at random that might help: http://www.rhinosoft.com/KnowledgeBase/kbarticle.asp?RefNo=1289

Basically you program the router so that any connection request at port 31350 will be forwarded to your computer on the lan at it's local IP address.

Good luck!

[original comment]

This is more of a comment than an answer (but I needed the extra room). Your try catch logic is going to make it much more difficult to diagnose the problem. Simplify the code as follows:

public static void main(String[] args) throws IOException, UnknownHostException
{
    Socket socket;

            //  #'s 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 include the exception stack trace. The original exception contains valuable information - if it says connection refused it means one thing - perhaps your port number is incorrect. If it says connection timed out it means something else - either you really do have a firewall problem or perhaps your ip address is wrong.

Your code is catching the useful exception, swallowing it and throwing a much less useful exception.

Do the same thing to your 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!