Game Networking Example/Tutorial
I built a top down two player death match game. However, due to the limited amount of key presses java can process it requires a game pad to play. So I'd like to rebuild it as a two player network game.
Can anyone provide me with a good piece of example code to show me the basics of how to lay it out? I have searched for examples on my own but I only ever come up with 'How to build a client/server chat program' which is entirely uselss and I can never figure out how to adapt it effectively to my needs. Thanks in advance.
# 1
> Can anyone provide me with a good piece of example
> code to show me the basics of how to lay it out? I
> have searched for examples on my own but I only ever
> come up with 'How to build a client/server chat
> program' which is entirely uselss
?
Why? The basic client server chat programs feature the basics of network programming. A simple protocol. Sending requests and recieving responses. Etc.
> and I can never
> figure out how to adapt it effectively to my needs.
Hmmmm.
Why don't you tell us how you see it working and I'll tell you whether you are going in the right direction and what to look at. This question is just way to open ended at present to be answered.
# 2
Well for one thing, I'd like to use UDP, mostly cause I hear it's more lightweight and better for games. Also I'm just not sure how to go about handling such a high amount of data being transfered. Do I need some kind of command stack or something? Basically, I just want it to tell the other player's computer what the first player is doing (i.e. position, angle). Also, my knowledge of network programming has vast and wonderous gaps and I'm basically just taking bits of example code and mashing it together like a monkey playing with play dough, so a push towards a good tutorial/book would be fantastic.
# 3
> Well for one thing, I'd like to use UDP, mostly cause
> I hear it's more lightweight
Yes UDP is "lighter". It sacrifices reliability for performance.
> and better for games.
Maybe.
> Also I'm just not sure how to go about handling such
> a high amount of data being transfered.
How much is a "high amount"?
>Do I need
> some kind of command stack or something?
I don't know what this means. Are you asking if you should make your own protocol?
> Basically, I
> just want it to tell the other player's computer what
> the first player is doing (i.e. position, angle).
> Also, my knowledge of network programming has vast
> and wonderous gaps and I'm basically just taking bits
> of example code and mashing it together like a monkey
> playing with play dough, so a push towards a good
> tutorial/book would be fantastic.
For tutorials I recommend http://java.sun.com/docs/books/tutorial/networking/index.html There is a section on UDP there (titled All About Datagrams)
For books I HIGHLY recommend http://www.amazon.com/Fundamental-Networking-Java-Esmond-Pitt/dp/1846280303
What I would suggest for you is the following.
Create a simple client server using datagrams or streams (TCP). Test with the amount of data you plan on sending. You don't have to actually create data just byte arrays of whatever size would be fine. But you should test what you want to do against the performance of what can be done.
Please note when you test it is CRITICAL that you test with two computers and not localhost otherwise you will not see any latency and will be misinforming yourself with regards to performance.