I've dealt with editing multiplayer game-servers that use a packet system, however I could never fully understand how it works, I understand that you would send a packetID from the Client/Server which contains the Data for that packet, however, I'm not sure how to go about sending/recieving it. Throughout my research I've found people doing it like this.
1: They would have a String that is converted to Bytes that is sent to the server, but it would have a number/seperator
- Example: String = "5;" + userName;
2: They would then create a string array using the Bytes recieved
- Example: String data = recieved.split(";");
3: They would then TryParse to get the number
- Example: Int packetID = Integer.TryParse(data);
4: They would then have a switch statement filled with all the various packet IDs.
This doesn't seem very efficient to me, however I'm just getting started with network programming (From scratch anyway) and I would like to have some peoples input here, I want to learn how to do this properly, I am developing in C#, however if you could post the code in Java or any other language that should be fine, because i should be able to easily convert it.
Thanks in advance, if you cannot supply code for me to learn from (Which is honestly, how I learn, from looking at basic, but completed code), then please try to explain exactly what I'm doing, I'm using Sockets in C# if that means anything to anyone, not sure how it differs from the TcpClient.