linux C epoll函数echo服务器编程

epoll函数和select也是一个IO多路复用函数,区别是epoll函数是linux内核特有的系统调用。

epoll函数通过创建一棵epoll红黑树,监听IO, epoll函数不用执行拷贝,当前进程和内核共享一块区域。存放epoll树。

epoll会确切通知哪些fd发送IO事件。

服务器

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>

const int BUF_SIZE = 100;
const int EPOLL_SIZE = 50;
void error_handing(char* message);

int main()
{ 
	int serv_sock;                      // 服务器sock
	int clnt_sock;                      // 客户端sock
	struct sockaddr_in serv_addr;       // 服务器地址
	struct sockaddr_in clnt_addr;       // 客户端地址
	// 创建
	serv_sock = socket(PF_INET, SOCK_STREAM, 0);
	if (serv_sock == -1)
		error_handing("socket() error");
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = 18888;                 // 端口
	serv_addr.sin_addr.s_addr = INADDR_ANY;    // ip
	// 绑定
	if (bind(serv_sock, (struct sockaddr*) & serv_addr, sizeof(serv_addr)) == -1)
		error_handing("bind() error");
	// 监听
	if (listen(serv_sock, 5) == -1)
		error_handing("listen() error");

	// 创建一棵epoll树
	int epfd = epoll_create(EPOLL_SIZE);      
	struct epoll_event event;
	struct epoll_event* ep_events = (struct epoll_event*)malloc(sizeof(event) * EPOLL_SIZE);

	event.events = EPOLLIN;
	event.data.fd = serv_sock;
	epoll_ctl(epfd, EPOLL_CTL_ADD, serv_sock, &event);
	while (1) {
		int event_cnt = epoll_wait(epfd, ep_events, EPOLL_SIZE, -1);
		if (event_cnt == -1) {
			puts("epoll_wait() error");
			break;
		}
		for (int i = 0; i < event_cnt; i++) {
			if (ep_events[i].data.fd == serv_sock) {
				socklen_t adr_sz;
				clnt_sock = accept(serv_sock, (struct sockaddr*) & clnt_addr, &adr_sz);
				event.events = EPOLLIN;
				event.data.fd = clnt_sock;
				epoll_ctl(epfd, EPOLL_CTL_ADD, clnt_sock, &event);
			}
			else {
				char buf[BUF_SIZE];
				int str_len = read(ep_events[i].data.fd, buf, BUF_SIZE);
				if (str_len == 0) {
					epoll_ctl(epfd, EPOLL_CTL_DEL, ep_events[i].data.fd, NULL);
					close(ep_events[i].data.fd);
					printf("closed client: %d \n", ep_events[i].data.fd);
				}
				else {
					write(ep_events[i].data.fd, buf, str_len);   //echo
				}
			}
		}
	}
	close(serv_sock);
	close(epfd);
	return 0;
}

void error_handing(char* message)
{
	fputs(message, stderr);
	fputs("\n", stderr);
	exit(1);
}

 客户端

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
const int  BUF_SIZE = 1024;
void error_handing(char* message);


int main()
{
	int sock;
	struct sockaddr_in serv_addr;
	// 创建socket
	sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sock == -1)
		error_handing("socket() error");
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = 18888;                 // 端口
	serv_addr.sin_addr.s_addr = INADDR_ANY;    // ip
	if (connect(sock, (struct sockaddr*) & serv_addr, sizeof(serv_addr)) == -1)
		error_handing("connect() error");
	else
		puts("Connected...");

	while (1) {
		fputs("Input message(Q to quit): ", stdout);
		char message[BUF_SIZE];
		fgets(message, BUF_SIZE, stdin);
		if (!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
			break;               // 按q或Q退出,其他发送字符串
		write(sock, message, strlen(message));
		int str_len = read(sock, message, BUF_SIZE);
		message[str_len] = '\0';
		printf("Message from server: %s", message);
	}
	close(sock);
	return 0;
}

void error_handing(char* message)
{
	fputs(message, stderr);
	fputs("\n", stderr);
	exit(1);
}

 

点赞