English 中文(简体)
Java Sockets在局域网而不是互联网上工作?
原标题:Java Sockets work over LAN but not Internet?

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

问题回答

当你与互联网上的 Java网络服务器连接时,它将使用服务器Socket和Socket。 这只是罚款。

互联网上的用户可能无法与你联系,因为你背着防火墙或路由/NAT(与 Java无关)。 在互联网上的用户通过IP地址在你的机器上打电话给港口之前,使用 Java不会使这项工作产生任何不同的影响。

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

http://en.wikipedia.org/wiki/Firewall_%28computing%29”rel=“noreferer” http://en.wikipedia.org/wiki/Port_forwarding”rel=“noreferer” http://en.wikipedia.org/wiki/IP_addressing”rel=“noreferer” IP Address in general ( 这将向您提供一些信息,说明IP如何处理工作、哪些是公共/私营工作,以及数据是如何通过网络转移的)。

从那以后,如果你把IP地址重新连接到<><><>><>><>>>和(公开或其他)“,正在发生什么错误信息(不仅仅是代码),以及你已经尝试和失败的话,那将真正有益。 关于中途停留的Q/A号拖车是一次难解的会议,我们确实需要具体的错误信息,如何配置你的港口,是否有防火墙等等,以及你所尝试的东西。 目前,鉴于你所贴出的法典,世界范围内存在许多潜在问题(尽管该法典可能实际上不是问题——它很可能是一个组合问题)。

我基本上说,虽然这是一个令人难以置信的广泛答案,但你的问题也令人难以置信,没有涉及一个明确的问题。

您需要来自您的ISP的静态IP地址,您需要将港口转至您使用的服务器的局域网IP。 注:有些ISP仅向商业客户提供静态IP。 与你的ISP核对。 并且确保你作为服务器使用的机器上的防火墙有你使用的港口。 我相信你们知道,因为你说,你在局域网上工作。 但图一将提及那些读过这段话的人,他们正在连接广域网和广域网。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签