UNIX TCP/IP :read: Transport endpoint is not connected read: Transport endpoint is not connected

15,245

You're trying to read the wrong socket. accept() returns a new socket and it is that new socket you should be reading the data from and writing data to.

Your code should do something more like this:

int readSocket = accept(sock ...);
if (readSocket == -1)
{
    // error
}
else
{
    // set up stuff and while loop
    read_len = read(readSocket....); // << Note which socket is being read

    // other stuff
}
Share:
15,245
thlgood
Author by

thlgood

Updated on June 09, 2022

Comments

  • thlgood
    thlgood almost 2 years

    I'm trying to use the following program to show the message recived form port 8888. I compiled the following code without any error and warning.

    After I run it, I use a broswer to open 127.0.0.1:8888

    Then, the console showed:

    read: Transport endpoint is not connected
    read: Transport endpoint is not connected
    

    I debug it, but I can't find the reason.

    platform

    Linux kernel 3.x Ubuntu 64bit

    code

    #include <stdio.h>
    #include <netdb.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <unistd.h>
    #include <netinet/in.h>
    //#include <errno.h>
    
    int main(int argc, char *argv[])
    {
        int sock;
        char buf[BUFSIZ+1];
    
        buf[BUFSIZ] = '\0';
        uint16_t port = (uint16_t)atoi("8888");
        struct sockaddr_in ser;
        memset(&ser, 0, sizeof(ser));
        ser.sin_port = htons(port);
        ser.sin_addr.s_addr = htonl(INADDR_ANY);
        ser.sin_family = AF_INET;
    
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sock < 0)
        {
            perror("socket");
            return -7;
        }
    
        /*Bind*/
        if (bind(sock, (struct sockaddr *)&ser, sizeof(ser)) < 0)
            return -2;
    
        /*listen*/
        if (listen(sock, 5) < 0)
            return -3;
    
        /*Accpet*/
        struct sockaddr_in cliAddr;
        socklen_t cliLen = sizeof(cliAddr);
        if (accept(sock, (struct sockaddr*)&cliAddr, &cliLen) < 0)
        {
            perror("accept");
            exit(1);
        }
        int read_len = 0;
        int i = 0;
    
        /*read and print*/
        while(1)
        {
            read_len = read(sock, buf, BUFSIZ);
            if(read_len < 0)
            {
                perror("read");
                break;
            }
            else
            {
                /*print buf*/
                while(i++ < read_len)
                    putchar(buf[i-1]);
                putchar('\n');
            }
            if(read_len != BUFSIZ)
                break;
        }
        return 0;
    }
    

    If you found any bad habits in my code, please tell me.

  • JeremyP
    JeremyP about 12 years
    @thlgood: Your code is reading the wrong socket. I'm adding a bit of an example to show you what you should be doing.