Archived

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

How to recv the whole packet at once

This topic is 5601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, Ok, for simplicities sake, i''m gonna show you some puesdo code: struct packet { char userName[30]; char userPass[30]; } // serverSide: //update function entry: send(openSocket,(char*)&packetStructure,sizeof(packetStructure)); //update function exit //clientSide: recv(openSocket,recvData,sizeof(recvData)); how do i access the userName w/o getting the whole structure at one time? (i.e.30 bytes of 60)

Share this post


Link to post
Share on other sites
first off. TCP has no idea of packets. there is no packets when dealing with TCP. send and recv calls dont match and you could have to use mulitple recvs just to get the 30 bytes or one may get to consective calls to send by the server. TCP is streambased thus you must deal with it as if you were reading from a file with the exception that recv will not always return the requested amount of data thus you MUST ALWAYS check the return value for the number of bytes actually recv''d and compensate this with more recv()s if required.

Share this post


Link to post
Share on other sites
yes, i''m already aware of that. My question here is HOW to handel the exceptions? You say, that i might have to use multipul recv''s, ok then, but how do i get all of the recv''d data into one structure?

Share this post


Link to post
Share on other sites
You continue to read all of your incoming data into a buffer. I presume that you''ve got some packet format set up so that once you get a packet header you know what packet it is, and the size.

Then you just keep reading into that buffer until you have enough data for a whole packet.

When you have a whole packet you remove it from the buffer and copy it to where the packet is needed.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Try continually reading in from the recv stream, but enqueuing data as it arrives. Check the queue in the main loop of your program, when it is exactly 1 the size of your data struct, flush it''s buffer enough to fill a newly created struct and leave the remaining data in the queue for the next recv call.

Freeware development:
http://www.ruinedsoft.com/

Share this post


Link to post
Share on other sites
That''s the problem - you can''t, as you''ll get split and joined structures coming in. You have to build them in a buffer until they''re big enough to take out and put in a structure.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites