鍍金池/ 問答/Java  C  Linux/ 服務(wù)器接收瀏覽器請(qǐng)求時(shí)候,對(duì)于epoll中EPOLLIN事件觸發(fā)兩次的疑惑?

服務(wù)器接收瀏覽器請(qǐng)求時(shí)候,對(duì)于epoll中EPOLLIN事件觸發(fā)兩次的疑惑?

我的關(guān)鍵代碼如下:

int main( int argc, char const *argv[] ) {
    // 網(wǎng)絡(luò)編程部分
    int server_sock = -1;
    u_short port = 0;
    int client_sock = -1;
    struct sockaddr_in client_name;
    int client_name_len = sizeof( client_name );
    pthread_t newthread;

    server_sock = startup( &port ); // 執(zhí)行startup函數(shù)之后,會(huì)隨機(jī)分配一個(gè)新的值給port。返回值的是已經(jīng)執(zhí)行了bind()的socket文件描述符
    printf( "httpd running on port %d\n", port );

    threadpool_t* pool = NULL; // threadpool_t是線程池的結(jié)構(gòu)
    assert( ( pool = threadpool_create( THREAD, QUEUE, 0 ) ) != NULL ); // 創(chuàng)建一個(gè)線程池
    fprintf( stderr, "Pool started with %d threads and "
            "queue size of %d\n", THREAD, QUEUE );
    
    // I/O多路復(fù)用
    struct epoll_event ev, events[20];
    int epfd;
    int nfds = 0; // 用來接收epoll_wait的返回值,表示非阻塞的文件描述符的數(shù)量

    epfd = epoll_create(256);
    setnonblocking(server_sock);
    ev.data.fd = server_sock;
    ev.events = EPOLLIN|EPOLLET; // 當(dāng)綁定的那個(gè)socket文件描述符可讀的時(shí)候,就觸發(fā)事件
    epoll_ctl(epfd, EPOLL_CTL_ADD, server_sock, &ev); // 把綁定的按個(gè)socket文件描述符添加到內(nèi)核的紅黑樹里面


    while ( 1 ) {
        nfds = epoll_wait(epfd, events, 20, 500);

        printf("有%d個(gè)請(qǐng)求到來\n", nfds);
        
    //     int i;
    //     for (i = 0; i < nfds; ++i) {
    //         if(events[i].data.fd == server_sock) { // 說明有新的客戶端連接到來了
    //             client_sock = accept( server_sock, ( struct sockaddr * )&client_name, &client_name_len );
    //             if ( client_sock == -1 ) {
                //     printf("error_die(accept)");
                //     error_die( "accept" );
                // }

                // setnonblocking(client_sock);
    //             // 往線程池中的任務(wù)隊(duì)列里面添加任務(wù)
    //             if ( threadpool_add( pool, &accept_request, (void*)&client_sock, 0 ) != 0 ) { // 添加一個(gè)任務(wù)到線程池結(jié)構(gòu)中的任務(wù)隊(duì)列里面
                //     printf( "Job add error." );
                // }
    //         }
    //     }
    }

    assert( threadpool_destroy( pool, 0 ) == 0 );
    close( server_sock );
    printf("server_sock close");
    return( 0 );
}

(有一些代碼是和線程池有關(guān)的,所以可以不看)
我先把這個(gè)Web服務(wù)器開啟,然后用瀏覽器去連接這個(gè)服務(wù)器,此時(shí),對(duì)于同一個(gè)連接,觸發(fā)了兩次EPOLLIN事件。
實(shí)驗(yàn)的圖片是這樣的:
圖片描述
然后我在瀏覽器查看網(wǎng)絡(luò)請(qǐng)求:
圖片描述
然后服務(wù)器的反應(yīng)是:
圖片描述
可以看出,我只用瀏覽器連接了一次服務(wù)器,但是server_sock這個(gè)綁定的socket文件描述符卻觸發(fā)了兩次EPOLLIN事件,我不是很清楚,所以希望前輩可以指點(diǎn)一下,謝謝。

再補(bǔ)充一個(gè)問題,如果讓server_sock為非阻塞的,那么在瀏覽器里面顯示HTML頁(yè)面就會(huì)非常慢(甚至不會(huì)顯示)

setnonblocking(server_sock);

而我去掉了setnonblocking(server_sock);之后,HTML頁(yè)面頁(yè)面就會(huì)很快的顯示。

下面有比較完整的代碼:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <assert.h>
#include "threadpool/threadpool.h"
#include "httpcore/httpcore.h"

int startup( u_short* );
void error_die( const char* );

#define THREAD 64
#define QUEUE  2000

int main( int argc, char const *argv[] ) {
    // 網(wǎng)絡(luò)編程部分
    int server_sock = -1;
    u_short port = 0;
    int client_sock = -1;
    struct sockaddr_in client_name;
    int client_name_len = sizeof( client_name );
    pthread_t newthread;

    server_sock = startup( &port ); // 執(zhí)行startup函數(shù)之后,會(huì)隨機(jī)分配一個(gè)新的值給port。返回值的是已經(jīng)執(zhí)行了bind()的socket文件描述符
    printf( "httpd running on port %d\n", port );

    threadpool_t* pool = NULL; // threadpool_t是線程池的結(jié)構(gòu)
    assert( ( pool = threadpool_create( THREAD, QUEUE, 0 ) ) != NULL ); // 創(chuàng)建一個(gè)線程池
    fprintf( stderr, "Pool started with %d threads and "
            "queue size of %d\n", THREAD, QUEUE );
    
    // I/O多路復(fù)用
    struct epoll_event ev, events[20];
    int epfd;
    int nfds = 0; // 用來接收epoll_wait的返回值,表示非阻塞的文件描述符的數(shù)量

    epfd = epoll_create(256);
    setnonblocking(server_sock);
    ev.data.fd = server_sock;
    ev.events = EPOLLIN|EPOLLET; // 當(dāng)綁定的那個(gè)socket文件描述符可讀的時(shí)候,就觸發(fā)事件
    epoll_ctl(epfd, EPOLL_CTL_ADD, server_sock, &ev); // 把綁定的按個(gè)socket文件描述符添加到內(nèi)核的紅黑樹里面


    while ( 1 ) {
        nfds = epoll_wait(epfd, events, 20, 500);

        printf("有%d個(gè)請(qǐng)求到來\n", nfds);
        
    //     int i;
    //     for (i = 0; i < nfds; ++i) {
    //         if(events[i].data.fd == server_sock) { // 說明有新的客戶端連接到來了
    //             client_sock = accept( server_sock, ( struct sockaddr * )&client_name, &client_name_len );
    //             if ( client_sock == -1 ) {
                //     printf("error_die(accept)");
                //     error_die( "accept" );
                // }

                // setnonblocking(client_sock);
    //             // 往線程池中的任務(wù)隊(duì)列里面添加任務(wù)
    //             if ( threadpool_add( pool, &accept_request, (void*)&client_sock, 0 ) != 0 ) { // 添加一個(gè)任務(wù)到線程池結(jié)構(gòu)中的任務(wù)隊(duì)列里面
                //     printf( "Job add error." );
                // }
    //         }
    //     }
    }

    assert( threadpool_destroy( pool, 0 ) == 0 );
    close( server_sock );
    printf("server_sock close");
    return( 0 );
}

/**********************************************************************/
/* This function starts the process of listening for web connections
* on a specified port.  If the port is 0, then dynamically allocate a
* port and modify the original port variable to reflect the actual
* port.
* Parameters: pointer to variable containing the port to connect on
* Returns: the socket */
/**********************************************************************/
int startup( u_short *port )
{
    int httpd = 0;
    struct sockaddr_in name; // 表示服務(wù)器的結(jié)構(gòu)體

    httpd = socket( PF_INET, SOCK_STREAM, 0 );
    if ( httpd == -1 )
        error_die( "socket" );
    memset( &name, 0, sizeof( name ) ); // 注意
    name.sin_family = AF_INET;
    name.sin_port = htons( *port );
    name.sin_addr.s_addr = htonl( INADDR_ANY ); // 即0.0.0.0
    if ( bind( httpd, ( struct sockaddr * )&name, sizeof( name ) ) < 0 )
        error_die( "bind" );
    if ( *port == 0 )  { /* if dynamically allocating a port */
        int namelen = sizeof( name );
        /*
        getsockname()函數(shù)用于獲取一個(gè)套接字的名字。
        它用于一個(gè)已捆綁或已連接套接字s,本地地址將被返回。
        本調(diào)用特別適用于如下情況:未調(diào)用bind()就調(diào)用了connect(),
        這時(shí)唯有g(shù)etsockname()調(diào)用可以獲知系統(tǒng)內(nèi)定的本地地址。
        在返回時(shí),namelen參數(shù)包含了名字的實(shí)際字節(jié)數(shù)。
        */
        if ( getsockname( httpd, ( struct sockaddr * )&name, &namelen ) == -1 )
            error_die( "getsockname" );
        *port = ntohs( name.sin_port );
    }
    if ( listen( httpd, 256 ) < 0 )
        error_die( "listen" );
    return( httpd );
}

/**********************************************************************/
/* Print out an error message with perror() (for system errors; based
* on value of errno, which indicates system call errors) and exit the
* program indicating an error. */
/**********************************************************************/
void error_die( const char *sc ) {
    printf( sc );
    perror( sc );
    exit( 1 );
}

/*
作用:設(shè)置文件描述符的狀態(tài)為非阻塞的
參數(shù)1:需要修改狀態(tài)的文件描述符
*/
void setnonblocking(int sock) {
    int opts;
    opts=fcntl(sock,F_GETFL);
    if(opts<0) {
        error_die("fcntl(sock,GETFL)");
    }
    opts = opts|O_NONBLOCK;
    if(fcntl(sock,F_SETFL,opts)<0) {
        error_die("fcntl(sock,SETFL,opts)");
    }
}
回答
編輯回答
故林

應(yīng)該是瀏覽器的鍋,為了提高速度自己先請(qǐng)求了一下,用 curl 試試

2018年1月23日 10:34
編輯回答
刮刮樂

樓上應(yīng)該不對(duì)吧? 都沒有建立連接. 怎么會(huì)有可讀可寫事件呢?

2018年1月15日 06:16
編輯回答
尛憇藌

第一次建立連接響應(yīng)一次,第二次是有可讀可寫狀態(tài)響應(yīng)一次。
代碼沒細(xì)看....

2018年1月10日 21:48