简体   繁体   English

为什么使用AF_UNIX系列的文件描述符会导致accept()给出“无效参数”错误?

[英]Why does using a file descriptor of the family AF_UNIX cause accept() to give an “invalid argument” error?

This is giving me an invalid argument error: 这给了我一个无效的参数错误:

int listenfd = socket(AF_UNIX, SOCK_STREAM, 0);
*clientfdp = accept(listenfd, (SA *) &clientAddr, &clientLen);

while this does not 虽然这不是

int listenfd = socket(AF_INET, SOCK_STREAM, 0);
*clientfdp = accept(listenfd, (SA *) &clientAddr, &clientLen);

But I need it to be AF_UNIX . 但是我需要它是AF_UNIX Whats the deal? 这是怎么回事? I have checked the errors everywhere as well. 我也检查了所有地方的错误。 The output of socket() is fine in either case, it is just during the accept . 无论哪种情况, socket()的输出都很好,它只是在accept期间。

Here is the code in its entirety: 以下是完整的代码:

// forward declarations
int error_msg( char * msg );
int usage( char name[] );

// a function to be executed by each thread
void * recv_log_msgs( void * arg );

// globals
FILE * log_fd; // opened by main() but accessible by each thread
typedef struct sockaddr SA;

int error_msg( char * msg )
{
    printf( "%s\n", msg );
    return -1;
}

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;
}

int usage( char name[] )
{
    printf( "Usage:\n" );
    printf( "\t%s <log-file-name> <UDS path>\n", name );
    return 1;
}

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

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

    pthread_t tid;

    int listenfd = socket(AF_UNIX, SOCK_STREAM, 0);     

    // 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);
        printf("%d",errno);
        return 0;
    }

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

    // close the log file
    fclose(log_fd);

    return 0;
}

the main() function fails to call pthread_join() after calling pthread_create() main()函数在调用pthread_join()后无法调用pthread_create()

The result is the process exits before the thread has a chance to write anything to the log file. 结果是进程在线程有机会将任何内容写入日志文件之前退出。

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

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