How to recv the whole packet at once

Started by
10 comments, last by TheTempest 21 years, 8 months ago
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)
"There are no such things as stupid questions...Just stupid people :)"-Me
Advertisement
recv(openSocket,recvData,sizeof(recvData)/2); maybe?


no, i dont belive that you understand my question, what i mean is. The client doesn''t know how much data is going to be missing...how would you recv that?
"There are no such things as stupid questions...Just stupid people :)"-Me
How about if you send the size to receive first so the
other side knows how many bytes to expect?


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
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.
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?
"There are no such things as stupid questions...Just stupid people :)"-Me
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
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/
___________________________Freeware development:ruinedsoft.com
Thanks, that''ll do.

As they say from where i come from, "Much grass" ;-)
"There are no such things as stupid questions...Just stupid people :)"-Me
Cool, post back to tell us how it goes!

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

This topic is closed to new replies.

Advertisement