int bytesSent, left, idx=0;
int bytesRecv = SOCKET_ERROR;
char sendbuf[DEFAULT_BUFFER] = "";
char recvbuf[DEFAULT_BUFFER] = "";
while(1)
{
printf("[The message you want to send to the server]:");
fgets(sendbuf, DEFAULT_BUFFER, stdin);
left = strlen(sendbuf);
while(left > 0)
{
bytesSent = send( m_socket, &sendbuf[idx], left, 0 );
//printf("%s was sent\n", sendbuf);
if(bytesSent == 0)
return ;
if(bytesSent == SOCKET_ERROR)
{
printf("send failed:%d", WSAGetLastError());
return ;
}
left -= bytesSent;
idx += bytesSent;
}
idx = 0;
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, DEFAULT_BUFFER, 0 );
//printf("%s is received\n", recvbuf);
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
return;
}
if (bytesRecv < 0)
return;
printf( "[The returned info from server]: %s\n", recvbuf);
}
bytesRecv = SOCKET_ERROR;
memset(sendbuf, 0, DEFAULT_BUFFER);
memset(recvbuf,0, DEFAULT_BUFFER);
}
发送数据的地方使用了一个while(left > 0)的循环,但是我想知道当left>0成立的时候,服务器端有消息发送过来,那么客户端是怎么运作的~谢谢