How to structure Packets for Client-Server communication
Question
I have created my client-server
application but there is still something confusing about the whole process. What is packets
? and what is the difference between sending a packet
and sending a message
(like i normally do).
So please, I expect a good explanation about:
- What is
packets
? - How to structure them?
- How to use them for communication in my
client-server
application?
Solution
For the sender and on a higher level a packet is equivalent to a message, you send packets or messages. On the receiving side a message may be split up into multiple packets. This splitting is most common using TCP connections, where you might have to do more than one receive-call (each returning a "packet") to receive a complete message.
To make matter more confusing, on a lower level a single TCP or UDP message (with TCP/IP headers prepended to the data) can also be called a packet. And on an even lower level an ethernet frame may be called a packet as well.
And to confuse even more, TCP connections has no real concept of message, it's just a stream of bytes. Messages are just an artificial concept on top of TCP. UDP on the other hand are distinct messages.
In short, it's probably simplest to just keep on using the term "message" when referring to the data you send or receive.