我想编写一个游戏,可以由坐在不同计算机前的多个玩家玩,这些计算机通过网络连接,我想使用 Java 来实现这一点。对此我有几个疑问。如果您能至少回答一个问题,我将非常高兴。
-
我可以想象两种不同的情况。在第一个中,我在计算机之间构建了本地网络。在第二种情况下,我使用互联网作为计算机之间通信的媒介。所以,我的第一个问题是,上述两种情况是否需要不同的方法(从软件的角度来看)。
-
第二个问题是计算机如何识别彼此。例如,两台计算机连接到互联网,理论上它们可以互相看到。但是第一台计算机如何知道如何引用第二台计算机呢?也许计算机需要知道彼此的 IP 地址?但如果 IP 地址是动态的怎么办?每次改变IP地址,连接就会丢失?
-
如果我使用本地网络,每台计算机是否都可以看到网络中的所有内容?
-
假设我的计算机知道另一台计算机的地址。它能用它做什么呢?我可以将文件发送到这台计算机吗?第二台计算机如何知道有东西发送给它?第二台计算机如何知道如何处理该文件?我是否应该在第二台计算机上运行一个程序,永久检查是否有东西到达,如果有东西到达(文件),程序应该知道如何处理它。
I would like to program a game that can be played by several players seating in front of different computer which are connected via network and I want to use Java for that. In this respect I have several questions. I will be happy if you can answer at least one the question.
-
I can imagine 2 different situations. In the first one I build a local network between the computers. In the second case I use Internet as a medium for communication between the computers. So, my first question is if the two above mentioned cases require different approaches (from the software point of view).
-
The second question is how computers can identify each other. For example, two computers are connected to Internet and, in theory, they can see each other. But how the first computer knows how to refer to the second one? Maybe computers need to know IP addresses of each other? But what if IP addresses are dynamic? Each time when IP address is changed the connection will be lost?
-
If I use a local network is it possible for every computer to see everything what is in the network?
-
Let's say my computer knows address of another computer. What can it do with that? Can I send a file to this computer? How the second computer will know that something is sent to it? How the second computer knows what to do with the file? Should I run, on the second computer, a program which permanently checks if something is arrived and, if something is arrived (a file), the program should know what to do with that.
发布评论
评论(3)
只要正确设置 ServerSocket 和套接字,您应该能够以相同的方式连接本地和非本地计算机。每台尝试连接到服务器计算机的计算机都应使用服务器的 IP 地址以相同的方式建立连接。 (您可以查看 Socket 的 java 文档)
如果您想识别用户而不是计算机,则可以建立登录设置。不确定这是否能满足您的需求,但这是一个识别用户的想法。
我认为这取决于“一切”是什么。
研究启用网络的 java 类(Socket、ServerSocket...)它们应该提供有关如何使用连接执行的操作的详细信息。
As long as you set up your ServerSocket and Sockets correctly, you should be able to connect both local and non-local computers in the same manner. Each computer trying to connect to your server computer should use the server's IP address to establish a connection in the same way. (You can look at the java docs for Socket)
You can institute a login setup if you want to identify users rather than computers. Not sure if this would meet your needs, but an idea for identification of users.
I think that would depend on what "everything" is.
Look into the java classes that enable networking (Socket, ServerSocket...) They should provide good details regarding what you can do with a connection.
从软件的角度来看,这两种情况是相同的。但是,您需要考虑代理和防火墙等因素。套接字通信可能在本地网络中工作,但在互联网上故障转移。
解决这个问题最简单的方法就是有一个服务器来协助您检测。所有用户都在服务器上注册,然后很容易被其他人检测到。每个系统只需要知道服务器的IP即可。
是的。再次考虑防火墙。
中央服务器也可以帮助解决这种情况。
These two cases are identical from software point of view. However, you need to take into considerations things like proxies and firewalls. Socket communication may work in a local network, but fail over the internet.
The easiest way to solve this is to have a server to assist you in detection. All users register with the server and then can be easily detected by others. Each system needs only to know the IP of the server.
Yes. Again take firewalls into consideration.
A central server can also help with this scenario.
我同意上面“熊会吃掉你”的评论,你可以从一些阅读/研究开始,例如:
在此处查看评论。
I agree with "Bears will eat you"'s comment above that you could start with some reading/research such as:
See reviews here.