What is wrong with my PacketRecv() function ?

Started by
3 comments, last by CoMaNdore 21 years, 2 months ago

  
int MySocket::RecvPacket(char *szBuff)
{
	int iRecv;
	int iLen = sizeof(paHead);

	iRecv = 0;
	while(iRecv < iLen)
	{	
		iRecv += Recv( &buffer[iRecv] , iLen-iRecv );
	}
	
	paHead head;
	memcpy(&head, buffer, iLen );
	
	iLen = head.iSize;
	while(iRecv < iLen)
	{
		iRecv += Recv( &buffer[iRecv] , iLen-iRecv );
	}

	memcpy(szBuff, buffer, iRecv);

	SUCC; // a macro for returing

}
  
- Me
Advertisement
quote:What is wrong with my PacketRecv() function ?
I think you are the one to tell, and we shall answer why it is wrong. You do not say anything about the problem; crash? nothing recieved? hangs? what?

Some reading: http://www.tuxedo.org/~esr/faqs/smart-questions.html

EDIT: Something weird is going on with that link
EDIT: Google's cached one is better



[edited by - CWizard on February 9, 2003 11:54:38 AM]
arg the forums f up; i had written a long new post ...

anyway ill make it short this time :

it hangs in the first loop for some strange reason.
and sometimes i get a 10054 error ...

and somethimes it works ... it apears to be random ...
- Me

  while(iRecv < iLen){iRecv += Recv( &buffer[iRecv] , iLen-iRecv );}  


Imagine what happens when Recv returns -1 (error, probably going to be WSAEWOULDBLOCK).

Try something like:


  while (iRecv < iLen){    int Recieved = Recv( &buffer[iRecv] , iLen-iRecv );    if (Recieved == -1)    {        if (WSAGetLastError() != WSAEWOULDBLOCK)        {            // fatal error (socket not ready etc)            return;        }    }    else if (Recieved == 0)    {        // other end terminated the connection        return;    }    else    {        iRecv += Recieved;    }}  


Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Error: 10054:

n existing connection was forcibly closed by the remote host.



Given that, I bet you it deals with the server closing the socket and you''re trying to recv data on a closed socket.

This topic is closed to new replies.

Advertisement