Recv调用获取空数据

Recv call get empty data

我正在学习使用 c 中的套接字。 这是我的代码:

void work_with_client(int client_sock){
    char buff[10] = {0};

    while(1){
            kv_log_message("\tWork with client\n");
            int is_received = recv(client_sock, buff, 10, 0); 
            if(is_received < 0){ 
                    perror("Received failed");
            } else {
                    printf("RESPONSE: %s\n", buff);
            }   
            printf("RESPONSE %d\n", is_received);

            sleep(1);
    }   
}

当我通过 telnet 连接到此服务器并立即断开连接时,服务器打印以下行:

    Work with client
RESPONSE: 
RESPONSE 10
    Work with client
RESPONSE: 
RESPONSE 10
    Work with client
RESPONSE: 
RESPONSE 10
    Work with client
RESPONSE: 
RESPONSE 10
    Work with client
RESPONSE: 
RESPONSE 1
    Work with client
RESPONSE: 
RESPONSE 0
    Work with client

而且我不明白为什么前 4 recv 次调用会获得完整大小的缓冲区数据(从客户端角度来看,我没有向套接字写入任何内容)。 有什么建议么? P.S。 运行 在 Mac OS X Yosemite

更新: 我在 Linux 上测试我的程序,并且在我的情况下 recv 调用总是 return 0,所以可能是 [=telnet 中的问题 Mac OS X

recv()返回0时,表示对方关闭了连接

来自man recv

RETURN VALUE

[...] The return value will be 0 when the peer has performed an orderly shutdown.

然后您可能想退出 while() 循环。


更新:

此外,代码可能会通过将非 0 终止的 "string" 传递给 printf().

来调用未定义的行为

要解决此问题,请少阅读 charbuff 然后它的大小并添加 0-终止符。

    while (1)
    {
        ssize_t is_received = recv(client_sock, buff, sizeof buff - 1, 0); 
        if (is_received < 0){ 
                perror("Received failed");
                break;
        } else if (is_received == 0) {
                printf("%s\n", "peer shutdown");
                break;
        }

        buff[is_received] = '[=10=]';
        printf("RESPONSE: %s\n", buff);
   }   

好的,我明白了,telnet on Mac OS X (Yosemite) 如果您只是关闭终端而没有通过 telnet 本身正确关闭连接,则发送一些数据 (即放置转义字符和 quit 命令)。

老实说,我不知道这是错误还是某种调试行为。