簡體   English   中英

如何在已經阻塞的情況下將C中的TCP Server從阻塞模式更改為非阻塞模式?或者如何正確關閉阻塞的TCP Server?

[英]How to change TCP Server In C from Blocking Mode to Non-Blocking Mode when it's already blocking Or How to shutdown a blocking TCP Server properly?

我在運行TCP Server時沒有問題,我喜歡它處於阻塞狀態,以避免無用的循環和休眠代碼以及無用的cpu周期。

在Linux環境中關閉它時會發生問題,它一直存在,直到所連接的用戶發送了一些東西,然后它才關閉。

我認為這是因為即使無盡的while循環設置為退出,它也會阻塞。 但是,當它阻止將套接字ID更改為NON_BLOCKING完全沒有幫助時,很可能必須在發生阻止之前將其設置為NON_BLOCKING。

#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h> /* Added for the nonblocking socket */

#define LISTEN_MAX 10       /* Maximum clients that can queue up */
#define LISTEN_PORT 32000
#define MAX_COMMANDS_AT_ONCE 4000
#define NANO_SECOND_MULTIPLIER  1000000  // 1 millisecond = 1,000,000 Nanoseconds

//Global so I can access these where I shut the threads off.
int listenfd, connfd; //sockets that must be set to non-blocking before exiting

int needQuit(pthread_mutex_t *mtx)
{
    switch(pthread_mutex_trylock(mtx)) {
        case 0: /* if we got the lock, unlock and return 1 (true) */
        pthread_mutex_unlock(mtx);
        return 1;
        case EBUSY: /* return 0 (false) if the mutex was locked */
        return 0;
    }
    return 1;
}

/* this is run on it's own thread */
void *tcplistener(void *arg)
{
    pthread_mutex_t *mx = arg;
    //keyboard event.

    SDLKey key_used;
    struct timespec ts;
    //int listenfd,connfd,
    int n,i, ans;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t clilen;
    pid_t     childpid;
    char mesg[MAX_COMMANDS_AT_ONCE];

    listenfd=socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    servaddr.sin_port=htons(LISTEN_PORT);
    int option = 1;
    if(setsockopt(listenfd, SOL_SOCKET,SO_REUSEADDR,(char*)&option,sizeof(option)) < 0)
    {
        printf("setsockopt failed\n");
        close(listenfd);
    }
    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

    listen(listenfd,LISTEN_MAX);

    while( !needQuit(mx) )
    {
        clilen=sizeof(cliaddr);
        connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);

        while( !needQuit(mx) )
        {
            n = recv(connfd,mesg,MAX_COMMANDS_AT_ONCE,0);

            if(n == 0 || n == -1) break;
            //...Do Stuff here with mesg...
        }
    }
    close(connfd);
}

close(connfd);
close(listenfd);
return NULL;
}

int main(int argc, char *argv[])
{
    /* this variable is our reference to the thread */
    pthread_t tcp_listener_thread;
    pthread_mutex_t mxq; /* mutex used as quit flag */

    /* init and lock the mutex before creating the thread.  As long as the
    mutex stays locked, the thread should keep running.  A pointer to the
    mutex is passed as the argument to the thread function. */
    pthread_mutex_init(&mxq,NULL);
    pthread_mutex_lock(&mxq);

    /* create a hread which executes tcplistener(&x) */
    if(pthread_create(&tcp_listener_thread, NULL, tcplistener, &mxq)) {
        fprintf(stderr, "Error creating TCP Listener thread\n");
        //clear thread for tcp listener on exit.
        /* unlock mxq to tell the thread to terminate, then join the thread */
        fcntl(listenfd, F_SETFL, O_NONBLOCK); /* Change the socket into non-blocking state  */
        fcntl(connfd, F_SETFL, O_NONBLOCK); /* Change the socket into non-blocking state    */

        pthread_mutex_unlock(&mxq);
        pthread_join(tcp_listener_thread,NULL);
        pthread_cancel(tcp_listener_thread);
        pthread_exit(NULL);
        return 0;
    }

    //End of the TCP Listener thread.


    // Waits 500 milliseconds before shutting down
    struct timespec ts;
    ts.tv_sec = 0;
    ts.tv_nsec = 500 * NANO_SECOND_MULTIPLIER;
    nanosleep((&ts, NULL);

    //Forces a shutdown of the program and thread.
    //clear thread for tcp listener on exit.
    /* unlock mxq to tell the thread to terminate, then join the thread */
    fcntl(listenfd, F_SETFL, O_NONBLOCK); /* Change the socket into non-blocking state  */
    fcntl(connfd, F_SETFL, O_NONBLOCK); /* Change the socket into non-blocking state    */
    pthread_mutex_unlock(&mxq);
    pthread_join(tcp_listener_thread,NULL);
    pthread_cancel(tcp_listener_thread);
    pthread_exit(NULL);
    return 0;
}

我嘗試了EJP這樣建議的修復程序,但仍未解決。.我已經將connfdlisternfd都設置為全局范圍

pthread_mutex_unlock(&mxq); 
    close(connfd); //<- this
    close(listenfd); //<-- this
pthread_join(tcp_listener_thread,NULL);
pthread_cancel(tcp_listener_thread);
pthread_exit(NULL);

您的偵聽器線程確實會阻塞accept()。

解決此問題(幾乎)的一種討厭方法是使用pthread_kill()將信號發送到偵聽器線程。 這將導致accept()返回errno == EINTR,對其進行測試然后返回。

但是,這有一個競態條件:如果在測試while(!needQuit(mx))條件與進入accept()之間接收到信號,則它將丟失,並且accept()將再次阻塞。

解決此問題的一種正確方法是使用諸如select()和管道的方法。 您可以選擇讀取管道和承插口。 當主線程希望偵聽器線程退出時,它將一個字節寫入管道。 偵聽器線程的select()調用在從管道讀取一個字節(在這種情況下退出)和/或可以接受客戶端時返回。

非阻塞套接字主要用於將大量套接字復用到一個事件循環(即線程)中。 對於服務器可伸縮性來說,這是一個好主意,但在此沒有必要。

要取消阻塞accept(),只需關閉監聽套接字即可。 確保accept()句柄周圍的代碼正確。

要取消阻止recv(),請關閉接收套接字以進行輸入。 這將導致recv()返回零,這必須再次正確處理。 否則,只需如上所述關閉套接字即可,如果您希望接收代碼知道您正在關閉應用程序,則可能會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM