服务器文章来源:https://www.toymoban.com/news/detail-619794.html
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define ERR(msg) \
do { \
printf("%s %s %d:", __FILE__, __func__, __LINE__); \
perror(msg); \
exit(-1); \
} while (0)
//端口号的网络字节序 1024~49151
#define PORT 6666 //设置端口号6666
#define IP "192.168.250.100"//设置本机IP
#define N 32
int main(int argc, char* argv[])
{
// 1.创建红黑树
int epfd = epoll_create(1);
if (epfd < 0) {
ERR("epfd err");
}
// 2.创建套接字
int socketfd;
if (-1 == (socketfd = socket(AF_INET, SOCK_STREAM, 0))) {
ERR("socket error");
}
// 3.struct epoll_event event
struct epoll_event event;
event.events = EPOLLIN | EPOLLET;
event.data.fd = socketfd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, socketfd, &event) < 0) {
ERR("epoll_ctl error");
}
// 填充服务器地址
struct sockaddr_in serveraddr, clientaddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(PORT);
serveraddr.sin_addr.s_addr = inet_addr(IP);
socklen_t serveraddr_len = sizeof(serveraddr);
socklen_t clientaddr_len = sizeof(clientaddr_len);
// 绑定
if (bind(socketfd, (struct sockaddr*)&serveraddr, serveraddr_len) < 0)
{
ERR("bind error");
}
// 监听
if (listen(socketfd, 10) < 0)
{
ERR("listen errorG");
}
int acceptfd, ret, i;
int bytes;
struct epoll_event events[20];
char buf[N] = {0};
while (1)
{
//使用epoll_wait 检测文件描述符是否发生
ret = epoll_wait(epfd, events, 20, -1);
if (ret > 0)
{
for (i = 0; i < ret; i++)
{
// 判断文件描述符数量
if (events[i].data.fd == socketfd)
{
// 判断对应文件描述符的IO类型
if (events[i].events & EPOLLIN)
{
acceptfd = accept(events[i].data.fd, (struct sockaddr*)&clientaddr, &clientaddr_len);
if (acceptfd < 0)
{
ERR("accept error");
}
printf("IP:%s 连接成功\n", (char*)inet_ntoa(clientaddr.sin_addr));
// 将新客户端通信套接字挂到树上
struct epoll_event event;
event.events = EPOLLIN | EPOLLET;
event.data.fd = acceptfd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, acceptfd, &event) < 0)
{
ERR("epoll_ctl err");
}
}
}
else
{
// 处理客户端信息
if (EPOLLIN & events[i].events == EPOLLIN)
{
memset(buf, 0, sizeof(buf));
if (-1 == (bytes = recv(events[i].data.fd, buf, sizeof(buf), 0)))
{
ERR("recv error");
}
else if (bytes == 0)
{
// 客户端断开信息
printf("acceptfd:%d 断开连接\n", events[i].data.fd);
// 关闭对应文件描述符
close(events[i].data.fd);
// 实现对于epoll的控制
epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
}
else
{
printf("buf:%s\n", buf);
}
if (-1 == send(events[i].data.fd, buf, N, 0))
{
ERR("send error");
}
}
}
}
}
else
{
ERR("epoll_wait error");
}
}
close(socketfd);
return 0;
客户端文章来源地址https://www.toymoban.com/news/detail-619794.html
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define ERR(msg) \
do { \
printf("%s %s %d:", __FILE__, __func__, __LINE__); \
perror(msg); \
exit(-1); \
} while (0)
#define N 32
//端口号网络字节序
#define PORT 6666
#define IP "192.168.250.100"
int main(int argc, const char* argv[])
{
// 1.创建套接字
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
ERR("socket error");
}
// 2.填充服务器网络信息结构体
struct sockaddr_in serveraddr;
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(PORT);
serveraddr.sin_addr.s_addr = inet_addr(IP);
socklen_t serveraddr_len = sizeof(serveraddr);
char buf[N] = {0};
int res = 0;
// 3.尝试与服务器建立连接
if (-1 == connect(sockfd, (struct sockaddr*)&serveraddr, serveraddr_len))
{
ERR("connect error");
}
printf("与服务器建立连接成功..\n");
while (1)
{
memset(buf, 0, sizeof(buf));
fgets(buf, N, stdin);
buf[strlen(buf) - 1] = '\0'; //清理结尾的\n
//发送数据
if (-1 == send(sockfd, buf, sizeof(buf), 0))
{
ERR("send error");
}
//接收服务器的应答信息
if (-1 == (res = recv(sockfd, buf, sizeof(buf), 0)))
{
ERR("recv error");
}
if (0 == res)
{
break;
}
//输出应答信息
printf("应答为:[%s]\n", buf);
}
//关闭套接字
close(sockfd);
return 0;
}
到了这里,关于2023.7.30(epoll实现并发服务器)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!