c - 为什么我的 UDS 服务器不接受连接?

标签 c multithreading sockets

我正在为一项作业编写一个小型 UDS 服务器,并且感觉我的代码大部分都在那里,但当我实际尝试运行它时,我完全没有得到任何结果。

代码通过 shell 脚本进行测试,该脚本向我的代码的主函数发送多个不同的测试调用。传递给 main 的唯一内容是 log文件名和 UDS path ,然后不同的客户端连接到服务器,这应该简单地产生特定的输出日志来检查服务器代码的正确性。

我的主要功能在这里:

int main( int argc, char * argv[] )
{
     if ( argc != 3 )
         return usage( argv[0] );

    log_fd = fopen(argv[1], "a");

    // create a server socket
    // domain (i.e., family) is AF_UNIX
    // type is SOCK_STREAM
    int listenfd = socket(AF_UNIX, SOCK_STREAM, 0);     

    socklen_t clientLength = sizeof(struct sockaddr_un);
    struct sockaddr_un clientAddr;
    clientAddr.sa_family = AF_UNIX;
    strcpy(clientAddr.sun_path, argv[2]);

    pthread_t tid;

    // unlink the UDS path)
    unlink(argv[2]);

    // bind the server socket
    bind(listenfd, (SA *)&clientAddr, clientLength);    

    // listen
    listen(listenfd, 1024);
    // loop to wait for connections;
    // as each connection is accepted,
    // launch a new thread that calls
    // recv_log_msgs(), which receives
    // messages and writes them to the log file
    while(1){
        printf( "Waiting for a connection on UDS path %s...\n", argv[2] );
        int * clientfdp = malloc(sizeof(int));
        *clientfdp = accept(listenfd, (SA *) &clientAddr, &clientLength);
        pthread_create(&tid, NULL, recv_log_msgs, clientfdp);
    }

    // when the loop ends, close the listening socket
    close(listenfd);        

    // close the log file
    fclose(log_fd);

    return 0;
}

其中用法是 myloggerd <log-file-name> <UDS path> .

此主函数监听任何客户端连接,当接受连接时,它会在名为 recv_log_msgs 的线程例程函数中创建一个新线程。 。我的代码在这里:

void * recv_log_msgs( void * arg ) //Thread Routine
{
    // loops to receive messages from a client;
    // when the connection is closed by the client,
    // close the socket
    int clientfd = *((int *)arg);
    char buffer[1500];
    memset(buffer, 0, 1500);
    int currentPos = 0;
    int bytesRec;
    int recvng = 1;

    while(recvng){
        bytesRec = recv(clientfd, buffer, 1500-currentPos, 0);
        currentPos += bytesRec;
        if(buffer[currentPos - 1] == '\n')
            recvng = 0;
    }

    fprintf(log_fd, "LOGGER %d %s", clientfd, buffer);
    close(clientfd);
    return NULL;
}

因此,每次线程进入此函数时,数据都会写入日志文件。如果日志文件结果正确,那么我得到 100%。如果没有,那我就失败了。

到目前为止,当我测试我的解决方案时,什么也没有发生。

我得到一个无限循环的打印,上面写着 Waiting for connection on the UDS path... ,位于 while 内在 main 的末尾。难道这不应该继续循环吗我已经调用accept并创建线程,直到该线程退出?

最佳答案

您与 AF_UNIX 套接字的 clientAddr 类型不匹配:必须是 struct sockaddr

这对我有用:

int main( int argc, char * argv[] )
{
     if ( argc != 3 )
         return -1;

//    log_fd = fopen(argv[1], "a");

    // create a server socket
    // domain (i.e., family) is AF_UNIX
    // type is SOCK_STREAM
    int listenfd = socket(AF_UNIX, SOCK_STREAM, 0);

    socklen_t clientLength = sizeof(struct sockaddr_un);
    struct sockaddr clientAddr;
    clientAddr.sa_family = AF_UNIX;
    strcpy(clientAddr.sa_data, argv[2]);

    pthread_t tid;

    // unlink the UDS path)
    unlink(argv[2]);

    // bind the server socket
    bind(listenfd, (struct  sockaddr*)&clientAddr, clientLength);

    // listen
    listen(listenfd, 1024);
    // loop to wait for connections;
    // as each connection is accepted,
    // launch a new thread that calls
    // recv_log_msgs(), which receives
    // messages and writes them to the log file
    while(1){
        printf( "Waiting for a connection on UDS path %s...\n", argv[2] );
        int * clientfdp = malloc(sizeof(int));
        *clientfdp = accept(listenfd, (struct sockaddr *) &clientAddr, &clientLength);
        pthread_create(&tid, NULL, recv_log_msgs, clientfdp);
        free(clientfdp);
    }

    // when the loop ends, close the listening socket
    close(listenfd);

    // close the log file
//    fclose(log_fd);

    return 0;
}

关于c - 为什么我的 UDS 服务器不接受连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35914726/

相关文章:

ruby - ruby 中的 fork 和线程

c# - C# 静态构造函数线程安全吗?

c++ - JNI 使用多线程从 C++ 调用 Java

c - 我如何 "decode"一个 UTF-8 字符?

c - 可靠地将相同的数据广播到 C 中的多个套接字

sockets - 从 C 套接字读取十六进制缓冲区时出现问题

android - 如何在 Android 应用程序中通过 UDP 套接字接收数据

c - 从交换链呈现会引发段错误

c++ - 在 Windows 上处理文件名中带有回车符的文件

c - TCP 函数 recv() 不能在循环中工作