Linux - send 出现 Resource temporarily unavailable

出现这个问题几分钟后又可以发送成功了。具体原因不知,初步猜测是发送频率太快缓冲区满了导致的。

int32_t CTCPHandler::sendbuffer(unsigned int sock,const void* buf, int32_t nlen)
{
	if(m_Socket <= 0){
		return 0;
	}
	int bytes;
	int32_t count =0;
	while ( count < nlen)
	{
#ifdef WIN32
		bytes= ::send(m_Socket, (char *)buf + count, nlen - count, 0);
		if( bytes == -1 || bytes == 0 )
		{
			if (WSAGetLastError() == WSAEWOULDBLOCK)
			{
#else
		bytes= ::send(m_Socket, (char *)buf + count, nlen - count, MSG_NOSIGNAL);
		if( bytes == -1 || bytes == 0 ){
			if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR){
				printf("Error: %s", strerror(errno));
#endif
                                usleep(1000);//发送缓冲区剩余0字节,延时等待发送
				return 0;
			}
			else
			{
				if (m_Socket)
				{
#ifdef WIN32
					closesocket(m_Socket);
#else
					close(m_Socket);
#endif
					m_Socket = 0;
				}
				return -1;
			}

		}
		count+= bytes;
	}
	return count;
}

猜你喜欢

转载自blog.csdn.net/qq_37990044/article/details/87780764