goddamnit recv()!!

Started by
6 comments, last by mattd 23 years, 6 months ago
i am using recv() in winsock 2.2 API and it dosen''t work...
    
void cSock::get(char* set)
{
	char temp[MYM4_MAXBUF];
	sprintf(temp, "\0");
	recv(sock, temp, sizeof(temp), 0);
	sprintf(set, "%s\0", temp);	
}
    
even with all this "\0"ing the string that is returned (char* set) is not terminated!! yearrghh! plz help... e.g.. char temp[MYM4_MAXBUF]; sock->get(temp); showMsg(temp); // <-- the message shown here will be not NULL terminated! -------------------------------------- "i too though nazrix was cool once. but then i saw the light -- mattd"
Advertisement
recv returns an int, which has the following meaning :

-1 ( => SOCKET_ERROR ) : an error occured,call WSAGetLastError to get the exact error code

0 : socket was already gracefully closed

> 0 : number of bytes received

So you can use the following call sequence :

int err= 0;

err= recv(
sock,
temp,
sizeof( temp ),
0);
switch( err )
{
SOCKET_ERROR : // error handling sequence
break;
0 : // can''t get any data
break;
default :
if( 0 < err )
{
temp[ err ]= 0;
}
else
{
MessageBox( "unhandled error occured during socket receiving !", MB_OK );
}
}

Empty your memory out. The compiler doesn''t do it for you.

Sample:
ZeroMemory(temp, MYM4_MAXBUF);

Make sure there is 1 extra char more then your message to retain a NULL character.
Two possible soultions:

1) Zero out your memory like suggested already.

2) The following:

iBytes = recv(socket,buffer,size,flags);

if( iBytes != SOCKET_ERROR ) { // Check real error codes here
buffer[iBytes+1] = ''\0'';
}


Number 2 would be faster since you dont need to zero out the buffer each time.
thanks, ill try the last suggestion by arrogantgod.

--------------------------------------
"i too though nazrix was cool once. but then i saw the light -- mattd"
i assume the reception is fine,
char temp[MYM4_MAXBUF];
for (int i = 0; i < MYM4_MAXBUF; i++)
temp = ''\0'';
recv(sock, temp, sizeof(temp), 0);

this it better


goto http://qsoft.cjb.net
then tutorials-->windows-->udp sockets programming
There is no need to zero out the memory, it is a waste of machine cycles. What Anon Poster posted (first reply) is correct. You need to look at the return values and terminate the string by hand.

Recv does not return a NULL terminated string. It returns the raw data in the buffer you specify. Since binary data can be sent that might include a NULL, having recv terminate the data with a NULL automatically just doesn''t make sense.

If you are going to terminate the string by hand, remember to pass in (sizeof (temp) - 1) into recv to reserve the final byte in temp for NULL termination. Otherwise, you get strange bugs that require much banging of your head against a wall to find. If you are going to ZERO the memory then call recv as a fast hack (which is ok), you still need to pass (sizeof (temp) - 1) to recv to preserve the final NULL in the string.

Tim
void cSock::get(char* set)
{
char temp[MYM4_MAXBUF];
int iLen = recv(sock, temp, sizeof(temp), 0);
temp[iLen] = ''\0'';
strncpy(set, temp, strlen(temp)+1);
}

This topic is closed to new replies.

Advertisement