Hi All!
I'm Writing a Winsock Server App, and Found, that one recv call just gives me 4 Byte of the Data the Client sent.
Thats really Confusing
So i'd like to know how to get the Whole Packet Data with one recv call.
Thanks
Hi All!
I'm Writing a Winsock Server App, and Found, that one recv call just gives me 4 Byte of the Data the Client sent.
Thats really Confusing
So i'd like to know how to get the Whole Packet Data with one recv call.
Thanks
>>one recv call just gives me 4 Byte of the Data
My best guess, and that's all it is as you have bothered posting any code, is that you're passing sizeof(mybuffer) to recv(), maybe like so:
bytes = recv(sd, buf, sizeof(buf), 0);
This will be a problem is buf is a pointer, rather than an array, as sizeof() will tell you the size of the pointer. buf may well be an array outside of your current function, but that isn't visible at the time sizeof() is used.
>>how to get the Whole Packet Data with one recv call.
You can't, ever, guarantee that. recv() will read as much data as there is available up to the size specified. If the network is flow, and the incoming packets are fragmented for whatever reason, your app will get the data in chunks. You are responsible for putting them back together into something meaningful, TCP/IP only guarantees getting the bytes from one end to the other in the correct order etc, it does not perform application level packet reconstruction.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>> My best guess, and that's all it is as you have bothered posting any code, is that you're passing sizeof(mybuffer) to recv()...
Hmm Yes your Right! - see:
recv(sClient, buffer, sizeof(buffer), 0);
Thx Hammer
>>You can't, ever, guarantee that.
Does this hold true if the "packet" is 1 byte? Just curious
>>how to get the Whole Packet Data with one recv call.
You can cheat
Code:int recv(SOCKET sock, char* buf, int packetSize, bool recvAll) { int bytesReceived = 0; int temp; while(bytesReceived < packetSize) { temp = recv(sock, &buf[bytesReceived], packetSize - bytesReceived, 0); if(temp == 0 || temp == SOCKET_ERROR || !recvAll) return temp; bytesReceived += temp; } return bytesReceived; }
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>>You can't, ever, guarantee that.
>>Does this hold true if the "packet" is 1 byte? Just curious
Well, on a blocking socket recv() will either return 1, 0 or -1.
: 1 - you got your byte
: 0 - the connection ended
: -1 - error
So, no, you can't guarantee it'll get you that one byte.
>>You can cheat
If you're receiving a char array that you intend to use as a string, just remember to make buf 1 bigger than you actually pass to that function. That way you have somewhere to put a \0.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]