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.
你们是否有任何想法可以发挥作用?
更有甚者:我使用服务器中的一种特殊线对接收、发送和接收袖珍材料,也使用用户发送和接收的具体线对面。 它发送了我提出并载有所有资料的物体。
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();
在这方面,我从客户那里发现,最重要的是:
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);
另外,我还有另一个问题,从1个客户到另一个客户,从3个或4个秒钟不等,这几乎是瞬时的,因为它在同一网络上,我有一个快的互联网。
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