通過(guò)epoll實(shí)現(xiàn)一個(gè)并發(fā)服務(wù)器
服務(wù)器
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#define ERR_MSG(msg) \
do { \
printf("LINE: %d\n", __LINE__);\
perror(msg); \
} while(0)
#define PORT 6666
#define IP "127.0.0.1"
int main(int argc,const char * argv[])
{
//創(chuàng)建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success\n");
//允許端口快速?gòu)?fù)用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("setsockopt success\n");
//填充地址信息結(jié)構(gòu)體
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//將IP地址和端口號(hào)綁定到指定的套接字文件描述符上
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//將通訊套接字設(shè)置為被動(dòng)監(jiān)聽(tīng)狀態(tài)
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
// 創(chuàng)建epoll句柄
int epfd = epoll_create(1);
if(epfd < 0)
{
printf("epoll_create filed\n");
return -1;
}
// 添加準(zhǔn)備就緒事件進(jìn)入epoll;
struct epoll_event event;
struct epoll_event events[10]; //存放就緒事件描述符的數(shù)組
event.events = EPOLLIN; // 讀事件
event.data.fd = sfd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event) < 0)
{
printf("epoll_ctl add filed\n");
}
//監(jiān)聽(tīng)事件是否發(fā)生
struct sockaddr_in cin; //存儲(chǔ)客戶端信息
socklen_t len = sizeof(cin);
struct sockaddr_in savecin[1024];
int ret, res;
char buf[128] = {};
while(1)
{
//如果成功,ret接收返回的事件個(gè)數(shù),把就緒的事件放在events數(shù)組中
ret = epoll_wait(epfd, events, 10, -1);
if(ret < 0)
{
printf("epoll_wait filed\n");
return -1;
}
//循環(huán)遍歷數(shù)組,做事件處理
for(int i=0; i<ret; i++)
{
if(events[i].events & EPOLLIN)
{
if(events[i].data.fd == sfd)
{
//觸發(fā)客戶端連接事件
int newfd = accept(sfd, (struct sockaddr*)&cin, &len);
if(newfd < 0)
{
ERR_MSG("accpet");
return -1;
}
printf("[%s:%d] 客戶端連接成功 newfd=%d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
savecin[newfd] = cin;
//添加新連接客戶端進(jìn)入epoll;
event.events = EPOLLIN; // 讀事件
event.data.fd = newfd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, newfd, &event) < 0)
{
printf("epoll_ctl add filed\n");
return -1;
}
}
else
{
//觸發(fā)客戶端交互事件
//接收數(shù)據(jù)
int cfd = events[i].data.fd;
bzero(buf, sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res == 0)
{
printf("[%s:%d] 客戶端下線 cfd=%d\n", inet_ntoa(savecin[cfd].sin_addr), ntohs(savecin[cfd].sin_port), cfd);
close(cfd); // 關(guān)閉文件描述符
//添加斷開(kāi)連接的客戶端從epoll中刪除;
if(epoll_ctl(epfd, EPOLL_CTL_DEL, cfd, NULL) < 0)
{
printf("epoll_ctl del filed\n");
return -1;
}
continue;
}
printf("[%s:%d] cfd=%d: %s\n", inet_ntoa(savecin[cfd].sin_addr), ntohs(savecin[cfd].sin_port), cfd, buf);
// 發(fā)送數(shù)據(jù)
strcat(buf, "*_*");
if(send(cfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
}
}
}
return 0;
}
客戶端
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) \
do { \
printf("LINE: %d\n", __LINE__);\
perror(msg); \
} while(0)
#define PORT 6666
#define IP "127.0.0.1"
int main(int argc,const char * argv[])
{
//創(chuàng)建流式套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success\n");
//填充地址信息結(jié)構(gòu)體
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//連接到服務(wù)器
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect server success\n");
char buf[128] = "";
ssize_t res = 0;
while(1)
{
//發(fā)送數(shù)據(jù)
bzero(buf, sizeof(buf));
printf("請(qǐng)輸入>>> ");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
if(send(cfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("發(fā)送成功\n");
//接收數(shù)據(jù)
bzero(buf, sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] 服務(wù)器下線\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
break;
}
printf("[%s:%d]: %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
}
close(cfd);
return 0;
}
運(yùn)行結(jié)果
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-619181.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-619181.html
到了這里,關(guān)于2023.07.29 驅(qū)動(dòng)開(kāi)發(fā)DAY6的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!