linux socket error Transport endpoint is not connected to trigger the timing of the recv shutdown

1.recv triggering

The wrong socket, the listening socket and connect socket misuse. Examples are as follows:

for(;;){
        socklen_t len = sizeof(client_address);
        connfd = accept(listenfd, (struct sockaddr *)&client_address, &len);
        if(connfd < 0){
            printf("accept error: %s\n", strerror(errno));
        }
        printf("server get connection from %s\n", inet_ntop(AF_INET, &client_address.sin_addr, buff, sizeof(buff)));

        if((n = read(listenfd, message, 1024)) == -1){
            printf("read error: %s\n", strerror(errno));
            exit(1);
        }
        message[n] = '\0';
        printf("receive the message of client: %s\n", message);
        close(connfd);

    }

Receiving a client link request above code for the server-side and displays information client pass over, Accept the function returns successfully a new descriptor kernel generated is assigned to connfd, so after the call to read () function, the first parameter should It is connfd instead listenfd. So the code line 9 should read

if((n = read(connfd, message, 1024)) == -1){ 

2.shutdown triggering

Case, the client socket has been shut down, the server socket call shutdown occurs this error.

 

Published 57 original articles · won praise 538 · Views 4.86 million +

Guess you like

Origin blog.csdn.net/whatday/article/details/104056667