为什么我的UDS服务器接受连接? [英] Why won't my UDS server accept connections?

查看:540
本文介绍了为什么我的UDS服务器接受连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个赋值小UDS服务器,并认为我的code主要是有的,但我得到绝对没有结果时,我其实尝试运行它。

I am writing a small UDS server for an assignment, and feel that my code is mostly there, but I am getting absolutely no results when I actually try to run it.

在code是由发送几个不同的测试呼叫到我的code的主要功能的shell脚本进行测试。传递给主的唯一的东西是登录文件名和 UDS路径,然后不同的客户端连接到服务器,它应该简单地导致一个特定的输出日志检查断绝code的正确性。

The code is tested by a shell script which sends several different test calls to the main function of my code. The only things passed to main are a log file name, and a UDS path, and then different clients connect to the server, which should simply result in a particular output log to check for correctness of the sever code.

我的主要功能是在这里:

My main function is here:

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<日志文件名称> < UDS路径方式>

该功能主要用于监听任何客户端连接,当一种技术被接受,它创建于 recv_log_msgs 称为线程程序的功能一个新的线程。我的code因为这是在这里:

This main function listens for any client connections, and when one is accepted, it creates a new thread at the thread routine function called recv_log_msgs. My code for that is here:

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%。如果没有,那么我会失败。

So, each time a thread enters this function, data is written to the log file. If the log file turns out correct, then I get a 100%. If not, then I fail.

截至目前,当我测试我的解决方案,没有任何反应都没有。

As of right now, when I test my solution, nothing happens at all.

我得到的说等待UDS路径上连接打印的无限循环...... ,这是里面的,而在主要结束。应该不是这个不能继续循环一个我所做的调用接受键,创建线程,直到该线程已退出?

I get an infinite loop of prints that say Waiting for connection on the UDS path..., which is inside the while at the end of main. Shouldn't this not continue to loop one I have made the call to accept and created the thread, until that thread has exited?

推荐答案

您是不匹配 clientAddr 为类型 AF_UNIX 插座:必须是结构sockaddr

You are mismatching the type of clientAddr for the AF_UNIX socket: have to be 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;
}

这篇关于为什么我的UDS服务器接受连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆