通過本文你會了解到:
1. 非阻塞預備知識點
2. 非阻塞server源碼
3. 運行測試(應用linux 的 nc 工具)
非阻塞預備知識點
O_NONBLOCK
- 應用於socket描述符時,另之後對此描述符的阻塞操作(如read/write等)變爲非阻塞。
EAGAIN
- 表示沒有可用數據,稍後再試。產生此錯誤的前提條件是socket描述符爲非阻塞時(即,設置爲O_NONBLOCK
標誌)。在VxWorks和Windows上,EAGAIN的名字叫做EWOULDBLOCK。
在非阻塞socket編程時,爲保證兼容性問題,最好同時判斷EAGAIN
和EWOULDBLOCK
標誌。
非阻塞server源碼
約定
格式爲 /**/ 的註釋對程序的主要流程進行解釋
格式爲 // 的註釋對程序做必要的說明
#include <stdio.h> // for printf()
#include <stdlib.h> // for exit()
#include <unistd.h> // for read() and write()
#include <sys/epoll.h> // for epoll
#include <sys/socket.h> // for epoll
#include <netinet/in.h> //struct sockaddr_in
#include <string.h> // memset
#include <fcntl.h> // fcntl
#include <errno.h> // errno
#define EPOLL_QUEUE_LEN 32 //監聽的最大連接數
#define BUF_SIZE 1024
static int set_nonblocking(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL, 0);
if(flags == -1) {
perror("fcntl");
return -1;
}
flags |= O_NONBLOCK;
if(fcntl(fd, F_SETFL, flags) == -1) {
perror("fcntl");
return -1;
}
return 0;
}
static int listen_socket(int port)
{
int fd;
struct sockaddr_in sa;
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd == -1) {
perror("socket");
return -1;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
perror("bind");
close(fd);
return -1;
}
if(set_nonblocking(fd) == -1) {
close(fd);
return -1;
}
if(listen(fd, 5) == -1) {
perror("listen");
close(fd);
return -1;
}
return fd;
}
int main(int argc, char **argv)
{
struct epoll_event ev;
struct epoll_event events[EPOLL_QUEUE_LEN];
int epfd;
int sfd;
int nfds;
if(argc != 2) {
printf("usage: %s [port]\n", argv[0]);
exit(EXIT_FAILURE);
}
/*創建 epoll 實例*/
epfd = epoll_create(EPOLL_QUEUE_LEN);
if(epfd == -1) {
perror("epoll_create");
exit(EXIT_FAILURE);
}
/*對輸入port進行監聽*/
printf("server listen form port: %d\n", atoi(argv[1]));
sfd = listen_socket(atoi(argv[1]));
if(sfd == -1) {
printf("listen_socket failed\n");
exit(EXIT_FAILURE);
}
/*將server描述符添加到epoll中*/
ev.data.fd = sfd;
ev.events = EPOLLIN | EPOLLET;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &ev) == -1) {
perror("epoll_ctl");
exit(EXIT_FAILURE);
}
/*主循環*/
while(1) {
int i;
/*等待epoll實例中描述符有I/O事件發生*/
nfds = epoll_wait(epfd, events, EPOLL_QUEUE_LEN, -1);
for(i = 0; i < nfds; i++) {
if(events[i].events & (EPOLLERR | EPOLLHUP)) {
//EPOLLERR - 出現錯誤
//EPOLLHUP - 客戶端提前關閉連接(close by peer)
continue;
}
if(!(events[i].events & EPOLLIN)) { //不是IN操作
continue;
}
if(sfd == events[i].data.fd) {
/*有客戶端連入server*/
struct sockaddr_in in_addr;
socklen_t in_len;
int infd;
while(1) { //非阻塞操作accept需要循環檢測
infd = accept(sfd, (struct sockaddr *)&in_addr, &in_len);
if(infd == -1) {
if(errno == EAGAIN || errno == EWOULDBLOCK) {
///已接收到所有描述符
break;
} else {
perror("accept");
break;
}
}
if(set_nonblocking(infd) == -1) {
break;
}
ev.data.fd = infd;
ev.events = EPOLLIN | EPOLLET;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, infd, &ev) == -1) {
perror("epoll_ctl");
exit(EXIT_FAILURE);
}
printf("incoming client [fd=%d]\n", infd);
}
} else {
/*收到客戶端數據*/
int done = 0;
while(1) {
ssize_t cnt;
char buf[BUF_SIZE];
memset(buf, 0, sizeof(buf));
cnt = read(events[i].data.fd, buf, sizeof(buf));
if(cnt == -1) {
if(errno == EAGAIN) {
done = 1;
break;
} else {
perror("read");
}
} else if (cnt == 0) { //fd 被關閉
done = 1;
break;
}
printf("receive data: %s\n", buf);
}
if(done == 1) {
printf("close client [fd=%d]\n", events[i].data.fd);
close(events[i].data.fd);
}
}
}
}
close(sfd);
return 0;
}
運行測試
linux中nc是一個強大的網絡工具,本文只用nc來創建一個socket客戶端,來測試epoll server,如果想深入瞭解nc可以參考linux man nc
。
nc創建客戶端命令:
接入server:nc ip port
- ip 爲server的IP地址 prot爲server的端口
當接入server後輸入數據並回車即可向server發送數據。
測試截圖: