Java – making multiplayer games via the Internet or the Internet

Hi, I wrote a multiplayer game in Java. I want to know what I need to learn and / or what I should use to make the game play through multiple computers on the network or the Internet Thank you. I'm really ignorant, because where to start, so any suggestion is helpful

Solution

Those other answers are quite high-level, which is good, but you don't want high-level, you want low-level, such as "how do I make it actually send data, what does this mean and what to send?" this is what you do:

1、 TCP or UDP? If you don't know what these things are, please read them, because I don't have space to give a good damage here, but your choices are as follows: TCP is good for round based games or games, because high latency is usually possible, because TCP guarantees packet transmission, so it may take some time to resend discarded packets This is good for things like chess or anything else

>UDP is good for games where you don't necessarily care about message reliability, and you prefer to just keep sending data. If you miss something, oh This is useful for games based on real-time operations, such as halo: reach or call of duty In these, if the location of the object is sent and the object will never be there, it is better to send a new location rather than resend an old location (now older), so it is not important to ensure reliability all the time In other words, you must have some reliability, so you still need something to ensure delivery, such as object creation and object destruction This means that you need to implement your own semi - reliable, priority - based protocol over UDP This is difficult

So ask yourself what's important, understand how TCP and UDP work, and then make a wise choice

That is, you must now synchronize the object state over the network This means that your object needs to be serialized into something that can be represented in the byte stream and written to the socket It is easy to write into the socket; If you can write a file, you can write a socket. It's really not difficult It is important to ensure that you can represent objects as buffers, so if an object has references / pointers to other objects, you will not be able to send these pointers because they are different on other clients, so you must convert them to something common to all hosts This means that although the ID of an object must be unique among all hosts, you must have a way to coordinate between hosts so that two hosts will not create different objects with the same ID There are ways to handle hosts that do this, but we don't have to worry about this (hint: use some mapping between host ID and network ID. bigger hint: don't do this if you don't need it)

So now you can send data, okay, now? Whenever the game state changes, you must send updates to other machines in other ways This is where the client - server architecture resides and can be peer - to - peer if needed Client - server is easier to implement In addition, a host "proxy" as a server is still a client - server, and anyone who says something different is wrong

Therefore, the responsibility of the server is to "own" all game states Only the server can say the state of an object with certainty If you want to move an object, you tell the server you want to move, but the server then tells you you should move the object, you don't just do it (although some client prediction is usually useful) Therefore, the server sends the updated object status to all other hosts

So, you mentioned a round game? It's simple:

>You are about to completely solve the client you are currently turning to Once the client does what they want to do, send the forwarded result to the server Then, the server verifies the client's movements (don't just trust the client and cheat in this way) and applies them to its object state. > Once the server is up to date, it will send messages to other clients in the world with new status, and these clients will apply these updates This includes customers whose turn has just come; The client should only update its world status when notified by the server, because you want to ensure consistency with other hosts, and you want to prevent hosts from cheating. > The server then sends a message indicating whose turn it is You can send the world status update in the previous step at the same time, which will be normal Just know that the customer's attempt is inappropriate This is why the server has authority over the world; If clients try to cheat, the server may knock them down

This is a round game you need to play Tip: bigger tip using TCP: TCP implements a tool called "Nagle's algorithm", which combines your messages into one packet This means that if you send two separate messages and two separate "send" calls, other hosts may only receive one packet and a single call "receive", but the packet will contain the packet sent by the content of both Therefore, if you send two 100 byte packets and two calls, you can receive a 200 byte packet in a single call This is normal, so you need to be able to handle this somehow One trick is to make each individual packet the same size, and then read only multiple bytes from the socket each time you check the input Remember, you can also get some messages For example, if two 100 byte messages are sent, they can be combined into a 200 byte message Next, if you read from the socket at the other end, but the buffer size you read is 150 bytes, there will be 150 bytes, including the first packet and part of the second packet You must call again to get the rest of the second message, so keep track of the amount of data you receive to avoid losing part of a packet That's why it's useful to keep your packets the same size

There are other useful techniques to reduce the size and frequency of your mail, track games that are not round based, and execute them in real time, but if you have a round based game, the right thing to do is probably to use TCP without worrying about anything else Here are some links to useful websites and articles that will provide you with more information on how to complete game network programming: http://www.gafferongames.com/ Glenn Fiedler's website, here are some good information 1500 archers' great paper on how to implement a technology called deterministic lock step, which is useful for many types of games

Let me know if you want more details, any of these things, or if you have more specific questions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>