Remotely connecting two non-local computers with sockets

Posted by Velizar Hristov on Stack Overflow See other posts from Stack Overflow or by Velizar Hristov
Published on 2012-09-16T21:06:00Z Indexed on 2012/09/16 21:37 UTC
Read the original article Hit count: 261

Filed under:
|

This question seems like something very obvious to ask, and yet I spent more than an hour trying to find an answer.

First I host and wait for someone to connect. Then, from another instance of the application, I try to connect with a socket - for the constructor, I use InetAddress, port. The port is always right, and everything works if I use "localhost" for the address. However, if I type my IP, I get an IOException. I even sent the application to someone else, gave him my IP, and it didn't work.

The aim of the application is to connect two computers. It's in Java. Here is the relevant code.

Server:

ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();

Client:

InetAddress a = InetAddress.getByName(ip);
Socket s = new Socket(a, port);

I don't get past that. Obviously, the values of int port and String ip are taken from text fields.

Edit: the purpose of my application is to connect two non-local computers.

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets