Im trying to make a server out of my computer so that clients from their computers can connect and communicate with my computer. I made the server on port 31350 and the client is trying to connect through my router's ip address. But it only works through lan when I have "localhost" or the name of my computer in the parameters for the socket creation. and not when I use my ip address, running the client and server on different networks. Here is the code.
Here is the server that my computer is running.
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 is running on his computer
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);
}
This is what it says when I run the client
Exception in thread "main" java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.(Socket.java:425) at java.net.Socket.(Socket.java:241) at ClientTest.main(ClientTest.java:22) // 22 is the line socket = new Socket(InetAddress.getByAddress(serverb),31350);
firewalls are disabled. the port 31350 on my router is forwarded to my computer's local ip address that I got from using ipconfig in cmd. But it still doesnt work, I just get IOException when trying to create the socket from the client computer. Nothing happens on the server computer. What's wrong?
[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