epoll/et例子

85 篇文章 0 订阅
78 篇文章 2 订阅
#include <sys/socket.h> 
#include <sys/wait.h> 
#include <netinet/in.h> 
#include <netinet/tcp.h> 
#include <sys/epoll.h> 
#include <sys/sendfile.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <strings.h> 
#include <fcntl.h> 
#include <errno.h> 
#define
MAX_EVENTS 10 
#define
PORT 8080 
//设置socket连接为非阻塞模式 
void
setnonblocking(int
			   sockfd) { 

				   int
					   opts; 

				   opts =
					   fcntl(sockfd,
					   F_GETFL); 

				   if(opts < 0) { 

					   perror("fcntl(F_GETFL)\n"); 

					   exit(1); 

				   } 

				   opts = (opts |
					   O_NONBLOCK); 

				   if(fcntl(sockfd,
					   F_SETFL,
					   opts) < 0) { 

						   perror("fcntl(F_SETFL)\n"); 

						   exit(1); 

				   } 
} 
int
main(){ 

	struct
		epoll_event ev,
		events[MAX_EVENTS]; 

	int
		addrlen,
		listenfd,
		conn_sock,
		nfds,
		epfd,
		fd,
		i,
		nread,
		n; 

	struct
		sockaddr_in local,
		remote; 

	char
		buf[BUFSIZ]; 

	//创建listen socket 

	if( (listenfd =
		socket(AF_INET,
		SOCK_STREAM, 0)) < 0) { 

			perror("sockfd\n"); 

			exit(1); 

	} 

	setnonblocking(listenfd); 

	bzero(&local, sizeof(local)); 

	local.sin_family =
		AF_INET; 

	local.sin_addr.s_addr =
		htonl(INADDR_ANY);; 

	local.sin_port =
		htons(PORT); 

	if(
		bind(listenfd, (struct
		sockaddr *) &local, sizeof(local)) < 0) { 

			perror("bind\n"); 

			exit(1); 

	} 

	listen(listenfd, 20); 

	epfd =
		epoll_create(MAX_EVENTS); 

	if (epfd == -1) { 

		perror("epoll_create"); 

		exit(EXIT_FAILURE); 

	} 

	ev.events =
		EPOLLIN; 

	ev.data.fd =
		listenfd; 

	if (epoll_ctl(epfd,
		EPOLL_CTL_ADD,
		listenfd, &ev) == -1) { 

			perror("epoll_ctl:
				listen_sock"); 

				exit(EXIT_FAILURE); 

	} 

	for (;;) { 

		nfds =
			epoll_wait(epfd,
			events,
			MAX_EVENTS, -1); 

		if (nfds == -1) { 

			perror("epoll_pwait"); 

			exit(EXIT_FAILURE); 

		} 

		for (i = 0;
			i <
			nfds; ++i) { 

				fd =
					events[i].data.fd; 

				if (fd ==
					listenfd) { 

						while ((conn_sock =
							accept(listenfd,(struct
							sockaddr *) &remote, 

							(size_t *)&addrlen)) > 0) { 

								setnonblocking(conn_sock); 

								ev.events =
									EPOLLIN |
									EPOLLET; 

								ev.data.fd =
									conn_sock; 

								if (epoll_ctl(epfd,
									EPOLL_CTL_ADD,
									conn_sock, 

									&ev) == -1) { 

										perror("epoll_ctl:
											add"); 

											exit(EXIT_FAILURE); 

								} 

						} 

						if (conn_sock == -1) { 

							if (errno !=
								EAGAIN &&
								errno !=
								ECONNABORTED 

								&&
								errno !=
								EPROTO &&
								errno !=
								EINTR) 

								perror("accept"); 

						} 

						continue; 

				}


				if (events[i].events &
					EPOLLIN) { 

						n = 0; 

						while ((nread =
							read(fd,
							buf +
							n,
							BUFSIZ-1)) > 0) { 

								n +=
									nread; 

						} 

						if (nread == -1 &&
							errno !=
							EAGAIN) { 

								perror("read
									error"); 

						} 

						ev.data.fd =
							fd; 

						ev.events =
							events[i].events |
							EPOLLOUT; 

						if (epoll_ctl(epfd,
							EPOLL_CTL_MOD,
							fd, &ev) == -1) { 

								perror("epoll_ctl:
									mod"); 

						} 

				} 

				if (events[i].events &
					EPOLLOUT) { 

						sprintf(buf, "HTTP/1.1
							200 OK\r\nContent-Length: %d\r\n\r\nHello World", 11); 

							int
							nwrite,
							data_size =
							strlen(buf); 

						n =
							data_size; 

						while (n > 0) { 

							nwrite =
								write(fd,
								buf +
								data_size -
								n,
								n); 

							if (nwrite <
								n) { 

									if (nwrite == -1 &&
										errno !=
										EAGAIN) { 

											perror("write
												error"); 

									} 

									break; 

							} 

							n -=
								nwrite; 

						} 

						close(fd); 

				} 

		} 

	} 

	return 0; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 epoll 读取串口数据的示例代码,供您参考: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <termios.h> #include <errno.h> #include <sys/epoll.h> #define MAX_EVENTS 10 #define BUF_SIZE 256 int set_interface_attribs(int fd, int speed) { struct termios tty; memset(&tty, 0, sizeof(tty)); if (tcgetattr(fd, &tty) != 0) { printf("Error %d from tcgetattr\n", errno); return -1; } cfsetospeed(&tty, (speed_t)speed); cfsetispeed(&tty, (speed_t)speed); tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls tty.c_cflag &= ~CSIZE; tty.c_cflag |= CS8; // 8-bit characters tty.c_cflag &= ~PARENB; // no parity bit tty.c_cflag &= ~CSTOPB; // only need 1 stop bit tty.c_cflag &= ~CRTSCTS; // no hardware flowcontrol // setup for non-canonical mode tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); tty.c_oflag &= ~OPOST; // fetch bytes as they become available tty.c_cc[VMIN] = 1; tty.c_cc[VTIME] = 1; if (tcsetattr(fd, TCSANOW, &tty) != 0) { printf("Error %d from tcsetattr\n", errno); return -1; } return 0; } int set_blocking(int fd, int should_block) { struct termios tty; memset(&tty, 0, sizeof(tty)); if (tcgetattr(fd, &tty) != 0) { printf("Error %d from tggetattr\n", errno); return -1; } tty.c_cc[VMIN] = should_block ? 1 : 0; tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout if (tcsetattr(fd, TCSANOW, &tty) != 0) { printf("Error %d setting term attributes\n", errno); return -1; } return 0; } int main() { int fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf("Error %d opening serial port\n", errno); return -1; } if (set_interface_attribs(fd, B9600) < 0) { printf("Error setting serial port attributes\n"); return -1; } if (set_blocking(fd, 0) < 0) { printf("Error setting serial port blocking mode\n"); return -1; } int efd = epoll_create1(0); if (efd == -1) { printf("Error %d creating epoll instance\n", errno); return -1; } struct epoll_event ev, events[MAX_EVENTS]; ev.events = EPOLLIN; ev.data.fd = fd; if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev) == -1) { printf("Error %d adding file descriptor to epoll instance\n", errno); return -1; } char buf[BUF_SIZE]; while (1) { int n = epoll_wait(efd, events, MAX_EVENTS, -1); if (n == -1) { printf("Error %d from epoll_wait\n", errno); break; } for (int i = 0; i < n; i++) { if (events[i].events & EPOLLIN) { int len = read(fd, buf, BUF_SIZE); if (len > 0) { buf[len] = 0; printf("Received %d bytes: %s\n", len, buf); } } } } close(fd); close(efd); return 0; } ``` 此示例代码使用 epoll 监听串口的 EPOLLIN 事件,当有数据可读时,使用非阻塞读取方式从串口读取数据。在读取到数据后,将其打印到控制台上。同时,该示例代码也设置了串口的基本参数,以确保能够正确地读取串口数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值