Right now, I’m programming the networking for my online game, and I’m not really sure
what to do about receiving data.
The problem is that I can’t really guess the packet’s size, so I thought of reading just 4 bytes from the packet and converting them to an int to know what’s the packet’s size.
Then I’ll just create a buffer in that size and receive the rest of the packet, is that a good idea?
For your information, I’m using non-blocking i/o.
Your approach sounds reasonable – you would essentially be embedding message size into a message header, which is likely the most robust way to handle it in your situation. Alternatively you could either use fixed length packets (ick) or use some sort of delimeter character (which will NOT work well at all for binary messages).
This link socket-protocol-fundamentals has some additional information which might help.