4

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

3 Answers3

7

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    Hi - as Peter Lawrey pointed out, your question probably has little to do with Java, and everything to do with network configuration. Please read up on Firewalls, NATs, etc. Please also consider getting a copy of "wireshark": http://www.wireshark.org. – paulsm4 Apr 10 '12 at 18:46
  • "flush() before creating the ObjectInputStream" ;) – Peter Lawrey Apr 10 '12 at 19:05
5

You need to read up on firewalls (which controls what is allowed to connect into a machine from outside the LAN), port forwarding (which can forward public IP addresses to internal IP addresses), and IP addressing in general (which will give you some information on how IP addresses work, what's public/private, and how data gets transferred across networks).

From there, it would be really helpful if, in future questions, you'd post what IP addresses you're connecting from and to (public or otherwise), what error messages are occurring (not just the code), and what you've already tried and failed at. The Q/A on stackoverflow isn't meant to be a troubleshooting session - we really need specific error messages, how your port forwarding is configured, what firewall(s) are in place, etc., and things you've tried. Currently, there are many potential issues with worldwide play given the code you've posted (though the code might not actually be the problem - it's probably more of a configuration issue).

I'm basically saying that while this is an incredibly broad answer, but your question is also incredibly broad, and doesn't involve a clear problem.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • Ok, but that means the connection didn't work - which we know. The cause of that could be **anywhere** in your firewall/port forwarding config, and could be on the client and/or server side, could be all clients or just some clients, etc. Unfortunately, it's not enough to give such a broad error message. You need to go through your config, testing each step of the connection along the way, figure out at **which step** the connection is failing, and start with a much more specific question from that point, with the related logs, errors, and config details. – jefflunt Apr 10 '12 at 18:47
  • Okay I allowed ports on firewall, ingoing and outgoing and port forwarded (again) now I'm going to test the game normally and over hamachi :) –  Apr 10 '12 at 18:50
0

You need a static IP address from your ISP and you need to forward the port in your router to point to the LAN IP of the server you are using ex. 192.168.1.xxx Note: some ISP's only provide static IP to business customers. check with your ISP. also make sure the firewall on the machine you are using as a server has the port you are using open. I'm sure you knew that because you said you have it working on the LAN. but i figured i would mention it for those who read this that are having trouble connecting over LAN and WAN.