[gameprogrammer] Re: Networking for games
- From: Olof Bjarnason <olof.bjarnason@xxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Mon, 9 May 2005 16:50:05 +0200
I don't know of any industry standard way for this - after all it is
your own protocol.
One way of doing this, which is easily re-used later on, is NOT
putting any "type" specifier in the header, ONLY the size and the
data. The type could then be put in the data instead - making re-use
of this protocol easy in the future.
Decide on a max-length first of all. Say you decide 200 bytes to be
max length. Then a one-byte-header is enough, and you could go with a
c structure like
struct Message {
unsigned char length;
unsigned char data[200];
};
.. and the corresponding read/write procedure prototypes:
int writeToSocket(SockStructure* sock, Message* msg);
int readFromSocket(SockStructure* sock, Message* msg);
Decide whether you want these to be blocking or non-blocking. If you
are already using threads, then blocking is quite feasible. If your in
a single-thread situation, non-blocking (ie. polling) is easier on the
responsiveness of the application.
Be careful in the implementation, especially in the readFromSocket()
procedure, since the whole message might not arrive and also two
messages in a row might be concatenated. [ use some helper
array/structure in order to store parts of arrived messages ..
memcpy() might be handy here ].
Good luck!
/Olof
On 5/9/05, Vince <uberneen@xxxxxxxxx> wrote:
> I'm working on a rogue-like that uses client-server
> architecture. I've come to the point where I need to
> worry about encapsulation and handling partial sends,
> etc. Right now when I send messages over the tcp
> connection the function loops until all the data is
> sent if it wasn't sent the first try, but I'm a little
> hung up on the receiving part. Originally I wanted to
> terminate commands with \n and figured that I'd loop
> through data until I had a complete command and no
> fragments. The more I think about it, the more I'm
> leaning toward sending the size of the message in
> bytes inside my own header. Ultimately I envision
> being able to send and recv with confidence using a
> [msg type][size][data] format.
> Is there an industry standard way of doing this, or is
> this sufficient? I'd like to use this framework in
> larger projects eventually so I'm looking for
> efficiency.
>=20
> Vince~
>=20
> Yahoo! Mail
> Stay connected, organized, and protected. Take the tour:
> http://tour.mail.yahoo.com/mailtour.html
>=20
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>=20
>
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- References:
- [gameprogrammer] Networking for games
- From: Vince
Other related posts:
- » [gameprogrammer] Networking for games
- » [gameprogrammer] Re: Networking for games
- » [gameprogrammer] Re: Networking for games
- » [gameprogrammer] Re: Networking for games
- » [gameprogrammer] Re: Networking for games
- » [gameprogrammer] Re: Networking for games
- » [gameprogrammer] Re: Networking for games
- » [gameprogrammer] Re: Networking for games
- » [gameprogrammer] Re: Networking for games
- [gameprogrammer] Networking for games
- From: Vince