Search code examples
csocketsunixrecv

Recv call get empty data


I'm learning to work with sockets in c. Here is my code:

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);
    }   
}

When I connect through telnet to this server and immediately disconnect, server print this lines:

    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

And I don't get why first 4 recv calls get full size of buffer data (I don't write anything to the socket from client perspective). Any suggestions? P.S. Run on Mac OS X Yosemite

UPDATE: i test my program on Linux, and recv call always return 0 in my case, so probably the problem in telnet on Mac OS X


Solution

  • Ok i get it, telnet on Mac OS X (Yosemite) send some data if you just close terminal without proper closing connection through telnet itself (i.e. put escape character and quit command).

    Honestly I don't know is it bug or some kind of debug behaviour.