简体   繁体   English

读取时未连接传输端点

[英]Transport endpoint is not connected on read

I am writing a POP3 server, and for some strange reason, it is only working on my computer and nobody else's. 我正在编写POP3服务器,出于某种奇怪的原因,它只能在我的计算机上工作,而没有其他人在工作。 It simply exited with the error "Transport endpoint is not connected". 它仅以错误“传输端点未连接”退出。 Google shows that most problems with the same error message are caused by passing the socket id instead of the connection id to read, but I did nothing of the sort, and I actually made the point to check that they are different, which is true every time I try to run it. Google展示了大多数相同错误消息的问题是由传递套接字ID而不是读取连接ID引起的,但是我没有做任何事情,实际上我是想检查它们是否不同,这是正确的我尝试运行它的时间。

void pop_protocol(int connection_descriptor){
    pthread_t tid;
    pthread_create(&tid, NULL, pop_worker, (void *) &connection_descriptor);
}

void *pop_worker(void *cd) {
    int connection_descriptor = *((int *) cd);
    socket_write(connection_descriptor, "+OK POP3 server ready\r\n");
}

int sockfd = socket_setup(portno);
while (1) {
    int connection = socket_get_new_connection(sockfd);
    pop_protocol(connection);
}

int socket_setup(int portno){
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
        fprintf(stderr, "Error: Cannot create socket\n");
        perror("");
        exit(1);
    }

    struct sockaddr_in address;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_family      = AF_INET;
    address.sin_port        = htons(portno);

    if (bind(fd, (struct sockaddr *) &address, sizeof(address)) == -1) {
        fprintf(stderr, "Error: Cannot bind socket to port %d\n", portno);
        perror("");
        exit(1);
    }

    if (listen(fd, MAXCONNECTION) == -1) {
        fprintf(stderr, "Error: Cannot listen on port\n");
        perror("");
        exit(1);
    }

    return fd;
}

int socket_get_new_connection(int sockfd){
    struct sockaddr_in address;
    socklen_t addrlen = sizeof(struct sockaddr_in);
    int new_socket = -1;
    int count = 0;
    while (new_socket < 0 && count < ACCEPTCOUNTER) {
        new_socket = accept(sockfd, (struct sockaddr *) &address, &addrlen);
    }

    // Attempt timed out. Exit
    if (count == ACCEPTCOUNTER) {
        fprintf(stderr, "Error: Cannot accept new connections");
        perror("");
        exit(1);
    }

    return new_socket;
}

I will be very grateful if anyone can offer any insight into the problem. 如果有人可以提供有关该问题的任何见解,我将不胜感激。

You pass a pointer to a local variable (parameter connection_descriptor ) to a new thread. 您将指向本地变量的指针(参数connection_descriptor )传递给新线程。 By the time the thread is created and executed, the pop_protocol function most probably returns, and the local variable is not valid any more (in fact, its value can still exist at the address that was passed to the new thread, but it's undefined behaviour.) 在创建和执行线程时,很可能会返回pop_protocol函数,并且局部变量不再有效(实际上,它的值仍然可以存在于传递给新线程的地址上,但这是未定义的行为) )

Try allocating memory for the connection_descriptor with malloc , passing that memory (not a local variable!) to the thread being created, and free that memory inside the thread's function after the socket id is copied into a local variable. 尝试使用mallocconnection_descriptor分配内存,将该内存(不是局部变量!)传递给正在创建的线程,并在将套接字ID复制到局部变量后在线程的函数内free该内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM