C++ / Socket design help

Soldato
Joined
7 Apr 2004
Posts
4,212
Hi,

Im using this sockets library with C++ http://www.alhem.net/Sockets/tutorial/using1a.html

This works in non-blocking event based style mode. So if i do the following:

Code:
mysockclass->Send("command1\r\n");
mysockclass->Send("command2\r\n");
mysockclass->Send("command3\r\n");

Then as a result the OnRead() method of mysockclass gets called 3 times whenever a response is sent back from the server.

This asynchronous design is all very nice, but im a little stuck as to how i process the response when it comes in.

Say when i send "command1" i need to process the response in OnRead() with specific regards to what was sent.

So if i send "command1" and the server sends back a string which OnRead() processes how can i tell it was from command1? (there is nothing specific in the server response that can tell me this).

With blocking sockets, i would do something like this:
Code:
Send(string1)
recv() // I would know what to expect in here
send(string2)
recv() // Again i would know what to expect
...


But how is this approach handled in an asynchronous design?

One idea i did have was having some kind of flag that would signal what i expect to receive and process the data according to that flag.

Thanks for any advice.

Jack
 
Doesn't this need to be handled a protocol level (I mean the protocol that you have designed), i.e. if you need to know what message the server is replying to, then it needs to package that with the response?
 
Yes, as Pulseammo says, If you have control of the server code, the responses should really contain some means of identifying them.
 
Thanks for the help guys i think that makes sense, i phraphs should have provided more server info.

The server is an NNTP server, which does send a 3 digit identification code i can use at the start of each response, however some responses exceed the buffer size of 1 read event, so the second/thrird/etc read from the socket is a remaining chunk of the first one with no form of ID i can use.

Increasing the buffer size wont help because i never know how many bytes will be sent in the response.

So if do:
mysockclass->Send("list\r\n");

I get a big response, which will cause several OnRead events to be raised. And because its async i would imagine i could get responses from another send command jumbled up in there, or will they all be read in the order they were sent?

So how would i approach 'joining' together multiple socket reads which really are all 1 response?

Thanks again and sorry if that doesn't make sense :p

Jack
 
You need to do some parsing of the data you read, to find the 3 digit ID/message code which delimits each message. Copy into a buffer after each read, check if the message is complete (i.e. search for delimiters), and if so, raise an event.

Obviously some book-keeping code is needed to keep track of where the current & next messages start in the buffer.

Also, even with blocking sockets, I don't see what you expect in your code example -- where are the calls to recv and send supposed to be from?
 
Back
Top Bottom