I started with UDP and wrote my own tcp-methodes, it was quite funny because I haven´t done something like this before.
Maybe you already know it but here are some abstract tipps.
- Your first 2 bytes of datagrampacket should be how large your packet is.
- You create another byte[] out of the data copying from 3 to the length (1)
- You got one big, I called it SendObject, object that includes the object you wanna send, the playerinformation, is TCP, object is splited, some numbers, etc.
- You use some threads:
for receiving,
analysing,
sending, take care, because there are some racing problems. I solved it with arrayLists. - Objects bigger than 64 kb are splited, the splited byte[] is the object in the “SendObject”, I create a hashmap with the number of the splited Object (I give every SendObject a number, SendObjects including splited Objects get the same number to put it back together, what part is it and how much parts are there. That´s faster than tcp because you don´t care about the order, but you have to make sure, that lost packets are send again.
- Don´t care about player positions, if they are lost, they are. It could be that packet 1 arrives after packet 2 -> you ignore packet 1
- If you have to care about the order, (I haven´t needed it yet), you just wait with sending packets until the receiver responded.
- Server-conntection:
Client klicks on “start server” -> server starts -> host connects
It´s a matter of opinion, but I did it like that, because in order it´s easy to create a “stand alone multiplayer-server” for a root-server.
Maybe some solutions aren´t perfect but I am just a try and error commander -> works well