C:pthread 无法在套接字上监听、绑定(bind)和接受

标签 c multithreading sockets network-programming

我正在尝试创建一个进程来监听套接字上的连接。当我在 main() 函数中绑定(bind)、监听和等待接受时,它似乎起作用了。但是当我尝试创建一个新线程并在该新线程上绑定(bind)、监听和接受时,它失败了。这是我的代码。

void request_handler(int clientSock) {
    FILE *requestedFile = NULL;
    long fileSize = 0;
    struct stat st;
    long bytesRead;
    char buffer[1024];

    requestedFile = fopen("/PATH/book.txt", "rb");

    while(!feof(requestedFile)) {
        bytesRead = fread(buffer, 1, sizeof(buffer), requestedFile);
        send(clientSock, buffer, bytesRead, 0);
    }

}

void listener() {
    int server_sock_desc;
    struct sockaddr_in name;

    int client_sock_desc;
    struct sockaddr_in client_name;
    socklen_t addr_size;

    pthread_t handler_thread;

    printf("waiting");

    //connection setup
    server_sock_desc = socket(PF_INET, SOCK_STREAM, 0);


    if(server_sock_desc != -1) {
        memset(&name, 0, sizeof(name));
        name.sin_family = AF_INET;
        name.sin_port = htons(5000);
        name.sin_addr.s_addr = htonl(INADDR_ANY);
        int bind_result = bind(server_sock_desc, (struct sockaddr *) &name, sizeof(name));
        if(bind_result == 0) {
            if(listen(server_sock_desc, BACKLOG) < 0) {
                perror("listen failed");
            }

            addr_size = sizeof(client_name);

            //Server Loop will continue to run listening for clients connecting to the server
            while(1) {

                //new client attempting to connect to the server

                client_sock_desc = accept(server_sock_desc, (struct sockaddr *) &client_name, &addr_size);
                if(client_sock_desc == -1) {
                    if(errno == EINTR) {
                        continue;
                    }
                    else {
                        perror("accept failed");
                        exit(1);
                    }
                }

                //connection starts here

                //create a thread for the new clients request to be handled
                if(pthread_create(&handler_thread, NULL, request_handler, client_sock_desc) != 0) {
                    perror("pthread_create failed");
                }
            }
        }
        else {
            perror("bind failed");
        }
    }
    else {
        perror("socket failed");
    }

}

int main(int argc, const char * argv[])
{
    pthread_t listenerThread;

    if(pthread_create(&listenerThread, NULL,listener, NULL) != 0) {
        perror("Listener thread create failed");
    }
}

奇怪的是,当我尝试通过调试器运行它时,有时listener()的一部分会执行,然后突然停止。

最佳答案

您需要给线程一个运行的机会。您的程序在创建线程后立即终止(通过从 main 返回)!

如果您希望初始线程终止并让其他线程继续运行,请调用 pthread_exit 而不是从 main 返回。如果您希望该线程等待直到监听线程终止,请在监听线程上调用 pthread_join

您让初始线程跑出 map 边缘。那里有龙。

关于C:pthread 无法在套接字上监听、绑定(bind)和接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377907/

相关文章:

c++ - 使用 qsort 导致段错误

c - fgetc 在读取文件时丢失字节

c++ - boost::log (boost logging): BOOST_LOG_FUNCTION 仅适用于主线程

c++ - 使用 g++ 和 clang++ 对 condition_variable 进行虚假唤醒

java DatagramSocket接收数据 组播Socket发送数据

sockets - 多个 TCP/IP 服务器并共享同一个 "well known port"......不知何故?

c++ - 套接字编程 简单聊天 C++

c - 我怎样才能跨越 union 的位域?

c - 引导加载程序如何在二进制文件中找到 DS 部分

c++ - 如何定义线程局部静态变量?