我的關(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ù)之后,會隨機分配一個新的值給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)建一個線程池
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)綁定的那個socket文件描述符可讀的時候,就觸發(fā)事件
epoll_ctl(epfd, EPOLL_CTL_ADD, server_sock, &ev); // 把綁定的按個socket文件描述符添加到內(nèi)核的紅黑樹里面
while ( 1 ) {
nfds = epoll_wait(epfd, events, 20, 500);
printf("有%d個請求到來\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ù)隊列里面添加任務(wù)
// if ( threadpool_add( pool, &accept_request, (void*)&client_sock, 0 ) != 0 ) { // 添加一個任務(wù)到線程池結(jié)構(gòu)中的任務(wù)隊列里面
// printf( "Job add error." );
// }
// }
// }
}
assert( threadpool_destroy( pool, 0 ) == 0 );
close( server_sock );
printf("server_sock close");
return( 0 );
}
(有一些代碼是和線程池有關(guān)的,所以可以不看)
我先把這個Web服務(wù)器開啟,然后用瀏覽器去連接這個服務(wù)器,此時,對于同一個連接,觸發(fā)了兩次EPOLLIN事件。
實驗的圖片是這樣的:
然后我在瀏覽器查看網(wǎng)絡(luò)請求:
然后服務(wù)器的反應(yīng)是:
可以看出,我只用瀏覽器連接了一次服務(wù)器,但是server_sock
這個綁定的socket文件描述符卻觸發(fā)了兩次EPOLLIN
事件,我不是很清楚,所以希望前輩可以指點一下,謝謝。
再補充一個問題,如果讓server_sock
為非阻塞的,那么在瀏覽器里面顯示HTML頁面就會非常慢(甚至不會顯示)
setnonblocking(server_sock);
而我去掉了setnonblocking(server_sock);
之后,HTML頁面頁面就會很快的顯示。
下面有比較完整的代碼:
#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ù)之后,會隨機分配一個新的值給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)建一個線程池
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)綁定的那個socket文件描述符可讀的時候,就觸發(fā)事件
epoll_ctl(epfd, EPOLL_CTL_ADD, server_sock, &ev); // 把綁定的按個socket文件描述符添加到內(nèi)核的紅黑樹里面
while ( 1 ) {
nfds = epoll_wait(epfd, events, 20, 500);
printf("有%d個請求到來\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ù)隊列里面添加任務(wù)
// if ( threadpool_add( pool, &accept_request, (void*)&client_sock, 0 ) != 0 ) { // 添加一個任務(wù)到線程池結(jié)構(gòu)中的任務(wù)隊列里面
// 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ù)用于獲取一個套接字的名字。
它用于一個已捆綁或已連接套接字s,本地地址將被返回。
本調(diào)用特別適用于如下情況:未調(diào)用bind()就調(diào)用了connect(),
這時唯有g(shù)etsockname()調(diào)用可以獲知系統(tǒng)內(nèi)定的本地地址。
在返回時,namelen參數(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)");
}
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。