2016-03-10 129 views
0

我正在爲一項任務編寫一個小型UDS服務器,並且覺得我的代碼大部分存在,但是當我實際嘗試運行它時,我得到的結果絕對沒有。爲什麼我的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在主結束。這不應該繼續循環一個我已經打電話給accept並創建線程,直到該線程退出?

+0

'如果(緩衝[currentPos - 1] == '\ n' )'不能工作。無論您是否找到換行符,都取決於您獲得的塊大小。 – usr

+0

你是什麼意思?該行取自我的教科書 編輯:但這是我的直接關注點,因爲我的代碼甚至沒有達到該功能。 – Anonymous

+0

注意'recv'可以在第一次迭代時返回'0'和'-1'。這意味着_Undefined Behaviour_ on'buffer [currentPos - 1]' – LPs

回答

0

你是不匹配的clientAddr類型爲AF_UNIX插座:必須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; 
} 
相關問題