How to recv the whole packet at once
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)
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?
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!
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?
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
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:
http://www.ruinedsoft.com/
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
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
Popular Topics
Advertisement