Archived

This topic is now archived and is closed to further replies.

Tancor

Async Questions.

Recommended Posts

Ok, I think I am beginning to understand, but I need a tinsy bit of help here. 1) Ok, this is more of an is this the way to handle it type of question... Situation: Allowed users 5 Users currently connected: 5 Need to do: reject any other connection requests. My thought right now in pseudo code is... accpet to a temporary socket. if numusers

Share this post


Link to post
Share on other sites
It seems like you have 3 questions, 2 of which I think I can give you good answers for

1) How should my "listening socket" deal with additional connections past my limit?

The method you discussed is generally the best way to do it (of course specific application issues could change that). One thing to be mindful of though: The OS will limit the number of connection *requests* you can have queued up on a socket at any given time. Also: Be careful how you handle the "over the limit" requests, if you have a thousand requests coming in one after the other you probably don''t want to be stuck in a loop handling them. Your "already connected clients" should have priority over handling these requests.


2) How do I make sure that all the data I send is received?

I''ll let someone else handle this one (I usually just put a message length at the beginning of every packet, but much more robust schemes exist).


3) How do I make my network data cross-platform compatible?

A variety of functions exist to port local data types into a generic "network byte order". Just lookup the htons() function for a good starting point. (This is only necessary if you intend to have your app running on different platforms)

Share this post


Link to post
Share on other sites
Ok, here are two more questions to the points you brought forth...

On part 1 - handling multiple connections.

Although I doubt this app would be dealing with 1000''s of connection requests, if it were caught in a loop, how would one "break" that loop to allow for processing for other clients?

On part 3 - htons -

I right now plan on leaving it as a M$ Windows based product right now (at least this particular program), so I guess doing a conversion isn''t important.

But, let''s say I was sending it to a system that required a conversion. if I have a structure of data that I am sending that is a mix of char, short, int, long, float, etc. how do I go about converting the entire packet? htons appears to be a short by short by short conversion (ie: if I have a packet that has 50 shorts in it, I would need to first get the packet to be one giant array of shorts, then I would need to call htons 50 times to convert each entry...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Serialize the whole thing to a stream of bytes first, they will always arrive in the same order you sent them...because they are only bytes

Share this post


Link to post
Share on other sites
Ok, so, how would I go about serializing them into an array of bytes?

I mean, I could screw around with something along the lines of...


NETMESSAGE = struct of message

NETMESSAGE Message
char *MsgPtr

MsgPtr = (char *)&Message

or something like that, but that still brings up the problem of... taking that array of bytes, putting it back into its original format, and checking if enough bytes have arrived...

Unless, is it possible to do on the receiving end...

NETMESSAGE *Message

char RecBuffer[512]

AmountReceived = amount of bytes received from read.

if AmountReceived >4 (assuming that the size component is in the first 4 bytes of the structure)

Message = (NETMESSAGE *)RecBuffer

And then check Message->Size == AmountReceived(assuming size was a component of the structure)
if not, keep adding to RecBuffer until it is?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you have a netmessage class, you should derive a different class for each different type of message, then each message type has it''s own functions to convert it''s data/variables to bytes and basically reverse the process to turn the bytes back into the netmessages variables on the other end.

Share this post


Link to post
Share on other sites
Good idea on classes. Right now I am doing this in C rather then C++, as I am not quite as comfortable with C++ to tackle this project. Although I wish I were, that method sounds very nice.

Share this post


Link to post
Share on other sites