Java Sockets work over LAN but not Internet?

user1217946 picture user1217946 · Apr 10, 2012 · Viewed 11.2k times · Source

Hello I am making a Java multiplayer game and everything works fine. It has all someone would need but I found a problem, it uses ServerSocket for server, and Socket for clients, works fine but the big problem is that it doesn't work over worldwide. Only LAN, I even tried Hamachi but that didn't work, too.

Do you have any ideas what would work?

Some more info: I use a specific thread in server for accepting, sending and receiving sockets and also specific thread in client for sending and receiving. It sends an object which I made and contains all information.

ip = InetAddress.getLocalHost().getHostAddress() + ":" + port;

server = new ServerSocket();

//This asks you if you want to use a specific IP or just the one that we got in first line
String socketaddress;
socketaddress = (String) JOptionPane.showInputDialog(null, "IP: ", "Info",JOptionPane.INFORMATION_MESSAGE,null,null,InetAddress.getLocalHost().getHostAddress());
server.bind(new InetSocketAddress(socketaddress, port));

//Here it starts the accept thread, and then it starts send and receive threads
new Thread(accept).start();

Here is stuff from client that I find most important:

socket = new Socket(ip,port);

String set_username = System.getProperty("user.name");
set_username = (String) JOptionPane.showInputDialog(null, "Username: ", "Info", JOptionPane.INFORMATION_MESSAGE,null,null,set_username);
username = set_username;

//It sends the username to server
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(username);

//Then server responds with a message
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String response = (String) ois.readObject();

//This writes the server' message   
JOptionPane.showMessageDialog(null, response, "Message",JOptionPane.INFORMATION_MESSAGE);

Also I have another problem, the sockets take like 3 or 4 seconds to get from 1 client to another and it should be almost instant because it's on same network and I have a fast internet.

EDIT: I tried creating a server on same pc with client and then when joining using my global IP, it didn't work :(

EDIT: ITS WORKING IM SO HAPPY RIGHT NOW ALL I HAD TO DO IS ADD PORTS IN FIREWALL THANK YOU SO MUCH GUYS :D. NOW I CAN PLAY WITH MY FRIENDS :3

Answer

Peter Lawrey picture Peter Lawrey · Apr 10, 2012

When you connect to a Java web server on the internet it will be using ServerSocket and Socket. This works just fine.

What probably doesn't work is that users on the Internet cannot connect to you because you are behind a firewall or router/NAT (nothing to do with Java). Until a user on the internet can telnet to a port on your machine by IP address, using Java will not make this work any differently.

BTW: You should always create your ObjectOutputStream AND flush() before creating the ObjectInputStream otherwise the ObjectInputStream on the other end can block forever.