(轉載)一個簡短的epoll伺服器示例, 監聽5000個埠, 使用執行緒池[safedead.cublog.cn]
阿新 • • 發佈:2019-01-22
/*Linux 2.6 x86_64 only*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <pthread.h>
#define THREAD_MAX 4096
#define LISTEN_MAX 5000
#define SERVER_IP "192.168.1.103"
typedef struct {
char ip4[128];
int port;
int fd;
} LISTEN_INFO;
//伺服器引數
static LISTEN_INFO s_listens[LISTEN_MAX];
//執行緒池引數
static unsigned int s_thread_para[THREAD_MAX][8];//執行緒引數
static pthread_t s_tid[THREAD_MAX];//執行緒ID
pthread_mutex_t s_mutex[THREAD_MAX];//執行緒鎖
//私有函式
static int init_thread_pool(void);
static int init_listen4(char *ip4, int port, int max_link);
//執行緒函式
void * test_server4(unsigned int thread_para[]);
int main(int argc, char *argv[])//客戶端驅動
{
//臨時變數
int i, j, rc;
int sock_listen; //監聽套接字
int sock_cli; //客戶端連線
int listen_index;
int epfd;
int nfds;
struct epoll_event ev;
struct epoll_event events[LISTEN_MAX];
socklen_t addrlen; //地址資訊長度
struct sockaddr_in addr4; //IPv4地址結構
//執行緒池初始化
rc = init_thread_pool();
if (0 != rc) exit(-1);
//初始化服務監聽
for(i = 0; i < LISTEN_MAX; i++) {
sprintf(s_listens[i].ip4, "%s", SERVER_IP);
s_listens[i].port = 8000 + i;
//建立監聽
rc = init_listen4(s_listens[i].ip4, s_listens[i].port, 64);
if (0 > rc) {
fprintf(stderr, "無法建立伺服器監聽於%s:%d/r/n", s_listens[i].ip4, s_listens[i].port);
exit(-1);
}
s_listens[i].fd = rc;
}
//設定集合
epfd = epoll_create(8192);
for (i = 0; i < LISTEN_MAX; i++) {
//加入epoll事件集合
ev.events = EPOLLIN;
ev.data.u32 = i;//記錄listen陣列下標
if (epoll_ctl(epfd, EPOLL_CTL_ADD, s_listens[i].fd, &ev) < 0) {
fprintf(stderr, "向epoll集合新增套接字失敗(fd =%d)/r/n", rc);
exit(-1);
}
}
//服務迴圈
for( ; ; ) {
//等待epoll事件
nfds = epoll_wait(epfd, events, LISTEN_MAX, -1);
//處理epoll事件
for(i = 0; i < nfds; i++) {
//接收客戶端連線
listen_index = events[i].data.u32;
sock_listen = s_listens[listen_index].fd;
addrlen = sizeof(struct sockaddr_in);
bzero(&addr4, addrlen);
sock_cli = accept(sock_listen, (struct sockaddr *)&addr4, &addrlen);
if(0 > sock_cli) {
fprintf(stderr, "接收客戶端連線失敗/n");
continue;
}
//查詢空閒執行緒對
for(j = 0; j < THREAD_MAX; j++) {
if (0 == s_thread_para[j][0]) break;
}
if (j >= THREAD_MAX) {
fprintf(stderr, "執行緒池已滿, 連線將被放棄/r/n");
shutdown(sock_cli, SHUT_RDWR);
close(sock_cli);
continue;
}
//複製有關引數
s_thread_para[j][0] = 1;//設定活動標誌為"活動"
s_thread_para[j][1] = sock_cli;//客戶端連線
s_thread_para[j][2] = listen_index;//服務索引
//執行緒解鎖
pthread_mutex_unlock(s_mutex + j);
}//end of for(i;;)
}//end of for(;;)
exit(0);
}
static int init_thread_pool(void)
{
int i, rc;
//初始化執行緒池引數
for(i = 0; i < THREAD_MAX; i++) {
s_thread_para[i][0] = 0;//設定執行緒佔用標誌為"空閒"
s_thread_para[i][7] = i;//執行緒池索引
pthread_mutex_lock(s_mutex + i);//執行緒鎖
}
//建立執行緒池
for(i = 0; i < THREAD_MAX; i++) {
rc = pthread_create(s_tid + i, 0, (void *)test_server4, (void *)(s_thread_para[i]));
if (0 != rc) {
fprintf(stderr, "執行緒建立失敗/n");
return(-1);
}
}
//成功返回
return(0);
}
static int init_listen4(char *ip4, int port, int max_link)
{
//臨時變數
int sock_listen4;
struct sockaddr_in addr4;
unsigned int optval;
struct linger optval1;
//初始化資料結構
bzero(&addr4, sizeof(addr4));
inet_pton(AF_INET, ip4, &(addr4.sin_addr));
addr4.sin_family = AF_INET;
addr4.sin_port = htons(port);
//建立SOCKET
sock_listen4 = socket(AF_INET, SOCK_STREAM, 0);
if (0 > sock_listen4) return(-1);
//設定SO_REUSEADDR選項(伺服器快速重起)
optval = 0x1;
setsockopt(sock_listen4, SOL_SOCKET, SO_REUSEADDR, &optval, 4);
//設定SO_LINGER選項(防範CLOSE_WAIT掛住所有套接字)
optval1.l_onoff = 1;
optval1.l_linger = 60;
setsockopt(sock_listen4, SOL_SOCKET, SO_LINGER, &optval1, sizeof(struct linger));
if (0 > bind(sock_listen4, (struct sockaddr *)&addr4, sizeof(addr4))) {
close(sock_listen4);
return(-1);
}
if (0 > listen(sock_listen4, max_link)) {
close(sock_listen4);
return(-1);
}
return(sock_listen4);
}
void * test_server4(unsigned int thread_para[])
{
//臨時變數
int pool_index; //執行緒池索引
int sock_cli; //客戶端連線
int listen_index; //監聽索引
char buff[32768]; //傳輸緩衝區
char *p;
int i, j, len;
//執行緒脫離建立者
pthread_detach(pthread_self());
pool_index = thread_para[7];
wait_unlock:
pthread_mutex_lock(s_mutex + pool_index);//等待執行緒解鎖
//執行緒變數內容複製
sock_cli = thread_para[1];//客戶端連線
listen_index = thread_para[2];//監聽索引
//接收請求
len = recv(sock_cli, buff, 32768, MSG_NOSIGNAL);
//構造響應
p = buff;
//HTTP頭
p += sprintf(p, "HTTP/1.1 200 OK/r/n");
p += sprintf(p, "Content-Type: text/html/r/n");
p += sprintf(p, "Connection: closed/r/n/r/n");
//頁面
p += sprintf(p, "<html>/r/n<head>/r/n");
p += sprintf(p, "<meta content=/"text/html; charset=UTF-8/" http-equiv=/"Content-Type/">/r/n");
p += sprintf(p, "</head>/r/n");
p += sprintf(p, "<body style=/"background-color: rgb(229, 229, 229);/">/r/n");
p += sprintf(p, "<center>/r/n");
p += sprintf(p, "<H3>連線狀態</H3>/r/n");
p += sprintf(p, "<p>伺服器地址 %s:%d</p>/r/n", s_listens[listen_index].ip4, s_listens[listen_index].port);
j = 0;
for(i = 0; i < THREAD_MAX; i++) {
if (0 != s_thread_para[i][0]) j++;
}
p += sprintf(p, "<H3>執行緒池狀態</H3>/r/n");
p += sprintf(p, "<p>執行緒池總數 %d 活動執行緒總數 %d</p>/r/n", THREAD_MAX, j);
p += sprintf(p, "</center></body></html>/r/n");
len = p - buff;
//傳送響應
send(sock_cli, buff, len, MSG_NOSIGNAL);
//釋放連線
shutdown(sock_cli, SHUT_RDWR);
close(sock_cli);
//執行緒任務結束
thread_para[0] = 0;//設定執行緒佔用標誌為"空閒"
goto wait_unlock;
pthread_exit(NULL);
}