开发者

How to start networking in Java?

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.

  1. 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).

  2. 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?

  3. If I use a local network is it possible for every computer to see everything what is in the network?

  4. 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.


  1. 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)

  2. 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.

  3. I think that would depend on what "everything" is.

  4. Look into the java classes that enable networking (Socket, ServerSocket...) They should provide good details regarding what you can do with a connection.


  1. 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.

  2. 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.

  3. Yes. Again take firewalls into consideration.

  4. 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:

How to start networking in Java?

See reviews here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜