c++socket通信的recv和send

int32_t os_socket::recvbuffer(void* buf, int32_t nlen)
{
	int bytes;
	int32_t count = 0;
	while (count < nlen)
	{
		bytes = recv(m_Socket, (char *)buf+count, nlen - count, 0);
		if( bytes == -1 || bytes == 0 ){
#ifdef WIN32
			if (WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINTR)
#else //WIN32
			if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
#endif //WIN32
			{
				bytes = recv(m_Socket, (char *)buf+count, nlen - count, 0);
				if( bytes == -1 || bytes == 0 ){
					return -1;
				}
			}else{
				return -1;
			}
		}
		count+= bytes;
	}
	return count;
}

 int32_t os_socket::sendbuffer(const void* buf, int32_t nlen)
 {
 	int bytes;
 	int32_t count =0;
 	while ( count < nlen){
 		bytes= send(m_Socket, (char *)buf + count, nlen - count, 0);
 		if( bytes == -1 || bytes == 0 ){
 #ifdef WIN32
 			if (WSAGetLastError() == WSAEWOULDBLOCK)
 #else
 			if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
 #endif
			{
				bytes= send(m_Socket, (char *)buf + count, nlen - count, 0);
				if( bytes == -1 || bytes == 0 ){
					return -1;
				}
			}else{
 				return -1;
			}

 		}
 		count+= bytes;
 	}
 	return (count >0 ? 0 : -1);
 }

你可能感兴趣的:(c++)