Advertisement

Async Questions.

Started by April 30, 2002 01:02 PM
5 comments, last by Tancor 22 years, 9 months ago
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 <maxusers Add user into user array. else Send message to user that server is full close the socket that was accepted Is this the correct way to handle it? 2) This is a bit more detailed, as I am not sure how to handle it... Now I know there are times where all the data for a complete packet may not be received (or sent from what I have read) So, what do I need to do to make a packet work? Let''s say my packet structure is: char Type (there would be <256 packet types) int PacketSize union of multiple sub types... type 1 char *MsgFrm char *Msg BOOL Private Type 2 short CommandID char *CommandParameters Type 4 char *LargeMOTD etc. Ok, so what kind of a recv function do I need to create to make sure I get the entire packet? I would prefer to use pointers, but as that may make it EXTREMELY difficult, I could use char arrays for my text messages, command parameters and motd messages, etc.. Do I have to do some sort of network conversion on all shorts/ints/etc. that are part of my packet and how do I do that? Just when I think I''m no longer confused...
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)
If a man is talking in the forest, and there is no woman there to hear him, is he still wrong?
Advertisement
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...
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
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?
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.
Advertisement
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.

This topic is closed to new replies.

Advertisement