#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#define EPOLL_SIZE 5
int main(int argc, char* argv[])
{
int client_fd;
int epoll_fd = 0;
int listen_fd;
int res = 0;
int opt = 1;
socklen_t clientaddr_len;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
int read_len = 0;
int i = 0;
char szbuff[1024];
if (argc < 2)
{
printf("usgae: %s port\n", argv[0]);
return -1;
}
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0)
{
perror("socket error");
return -1;
}
/* 端口重复利用 */
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(opt));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[1]));
serveraddr.sin_addr.s_addr = INADDR_ANY;
res = bind(listen_fd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));
if (res != 0)
{
perror("bind error");
return -1;
}
res = listen(listen_fd, 10);
if (res != 0)
{
perror("listen error");
return -1;
}
clientaddr_len = sizeof(struct sockaddr_in);
//epoll_create参数只需大于0就可以,没有意义
epoll_fd = epoll_create(EPOLL_SIZE);
struct epoll_event ev;
struct epoll_event *ep_events = NULL;
ep_events = (struct epoll_event*)malloc(sizeof(struct epoll_event)*EPOLL_SIZE);
if (ep_events == NULL)
{
perror("malloc");
return -1;
}
memset(&ev, 0x00, sizeof(struct epoll_event));
memset(ep_events, 0x00, sizeof(struct epoll_event));
ev.events = EPOLLIN;
ev.data.fd = listen_fd;
res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev);
if (res != 0)
{
perror("epoll_ctl error");
free(ep_events);
return -1;
}
while(1)
{
res = epoll_wait(epoll_fd, ep_events, EPOLL_SIZE, -1);
if (res > 0)
{
for (i = 0; i<res; i++)
{
if (ep_events[i].events & EPOLLIN)
{
/* 监听套接字有连接请求 */
if (ep_events[i].data.fd == listen_fd)
{
client_fd = accept(listen_fd, (struct sockaddr*)&clientaddr, &clientaddr_len);
if (client_fd < 0)
{
perror("client_fd error");
free(ep_events);
return -1;
}
printf("client_fd=%d has connect success\n", client_fd);
//将新连接的文件描述符加入到epoll_fd中
ev.events = EPOLLIN | EPOLLET;//水平触发
ev.data.fd = client_fd;
res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
if (res != 0)
{
perror("epoll_ctl error");
free(ep_events);
return -1;
}
}
else /* 对就绪状态读套接字数据进行读取*/
{
memset(szbuff, 0x00, sizeof(szbuff));
read_len = read(ep_events[i].data.fd, szbuff, sizeof(szbuff)-1);
if (read_len == 0)
{
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, ep_events[i].data.fd, ep_events);
close(ep_events[i].data.fd);
printf("fd=%d has closed\n", ep_events[i].data.fd);
continue;
}
else if (read_len < 0)
{
free(ep_events);
return -1;
}
printf("fd=%d, msg=%s\n", ep_events[i].data.fd, szbuff);
//将描述符修改为可写
ev.events=EPOLLOUT;
ev.data.fd = ep_events[i].data.fd;
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, ep_events[i].data.fd, &ev);
}
}
//可写操作后,将描述符修改为可读
if (ep_events[i].events & EPOLLOUT)
{
write(ep_events[i].data.fd, "OK", 2);
ev.events=EPOLLIN;
ev.data.fd = ep_events[i].data.fd;
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, ep_events[i].data.fd, &ev);
}
}
}
}
free(ep_events);
close(listen_fd);
return 0;
}
linux c epoll实例
最新推荐文章于 2024-07-22 08:19:17 发布