2 u012411289 u012411289 于 2014.09.19 20:35 提问

刚接触c/s架构,新手求教~TX

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成立的时候,服务器端有消息发送过来,那么客户端是怎么运作的~谢谢

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!