recv() return

Hello everyone,
can somebody explain how recv() function really works? As I understand now it waits for data to be in buffer in TCP level and takes the maximum amount of the third argument of it. But then the problem arises - i use the same function two times in different programs (server and client) in from first look the same ways, but in server it returns the amount expected, while in client it is always 31 bytes no matter what 3rd argument is. And, consequently, the data is not received fully. What could be the cause of it? Or what I don't understand about this function? Thank You.

EDIT: the server side function does not return expected value too...
Last edited on
You probably have an error in your code, so it would be best to show how you invoke recv.
Are you aware that you usually will have to call recv repeatedly to receive all data?
Here it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#define TEMP_BUFFER_SIZE 1024
...
(hSocked created, conected etc...)
_beginthread(priimti, 0, (void*)&hSocket);
...
void priimti(void *skt) {
	char tempBuffer[TEMP_BUFFER_SIZE];
	string zn;
	SOCKET hSocket = *(SOCKET*)skt;
	int retval;
	while(true)	{
		retval = recv(hSocket, tempBuffer, 1024, 0);
		cout<<"received: "<<retval<<" "<<tempBuffer<<endl;
		if (retval==0) { 
			break;
		}
		else if (retval==SOCKET_ERROR) {
			writeout("Socker error while receiving data.");
			break;
		}
		else {
			tempBuffer[retval] = '\0';
			zn = tempBuffer;
			writeout(zn);
		}
	}
	_endthread();
}

The cout line always indicates that retval is exactly 31 and therefore the message is cut if it is sent longer than 31 bytes.
Last edited on
Instead of appending the data you receive, you overwrite the old data with the new one.
So yeah, of course you won't receive everything.
That does not explain why in the other program with almost identical call the retval is always 128.

And i don't really understand why i should not receive everything if i do not store that data, but simply output in the screen? (That writeout simply couts a string and does some fancy tricks not related to receiving)

And if recv takes data in portions then why doesn't it take the rest of data? (Although i assume recv takes the exact data sent by send() function)
Last edited on
And if recv takes data in portions then why doesn't it take the rest of data? (Although i assume recv takes the exact data sent by send() function)

That could be because the data has not been received yet. recv will give you the data in arbitrary portions and your program needs to handle that correctly.
Perhaps you're sending the data in portions of 32 and 128 bytes? Either way, all data you're sending will be received on the other end - just not necessarily in the same number of fragments.
You actually were right! Thank you very much! Now when it's so obvious it's a surprise how i didn't thought of that... :D i had set wrong amount of data to send, because i used sizeof instead of static constant.
Topic archived. No new replies allowed.