当前位置:网站首页>Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
2022-04-23 14:40:00 【m0_ fifty-one million five hundred and fifty-one thousand three】
1、I/O Another use of reuse : At the same time processing TCP and UDP service
Same port ( or socket Address ) Can receive UDP Requests can also be received TCP request . We use the same socket Create two addresses socket File descriptor , Respectively used to process the data on this port UDP and TCP request . And then use I/O Multiplexing technology listens to both socket File descriptor events , You can handle the data on one port at the same time TCP and UDP request .
2、 Example program
The following is a server program , He can handle... At the same time TCP and UDP service .
/* This program implements a simultaneous processing TCP Request and UDP The requested echo server */
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<sys/epoll.h>
#include<pthread.h>
#include<errno.h>
#define MAX_EVENT_NUMBER 1024
#define TCP_BUFFER_SIZE 512
#define UDP_BUFFER_SIZE 1024
int setnonblocking(int fd)
{
int old_option = fcntl(fd, F_GETFL);
int new_option = old_option | O_NONBLOCK;
fcntl(fd, F_SETFL, new_option);
return new_option;
}
void addfd(int epollfd, int fd)
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
setnonblocking(fd);
}
int main(int argc, char* argv[])
{
if (argc <= 2)
{
return 1;
}
const char* ip = argv[1];
int port = atoi(argv[2]);
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_port = htons(port);
address.sin_family = AF_INET;
inet_pton(AF_INET, ip, &address.sin_addr);
/* establish TCP socket, And bind it to the port port On */
int listenfd = socket(PF_INET, SOCK_STREAM, 0);
assert(listenfd >= 0);
int ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address));
assert(ret != -1);
ret = listen(listenfd, 5);
assert(ret != -1);
/* establish UDP socket, And bind it to the port port On */
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_pton(AF_INET, ip, &address.sin_addr);
int udpfd = socket(PF_INET, SOCK_DGRAM, 0);
assert(udpfd >= 0);
ret = bind(udpfd, (struct sockaddr*)&address, sizeof(address));
assert(ret != -1);
epoll_event events[MAX_EVENT_NUMBER];
int epollfd = epoll_create(10);
assert(epollfd != -1);
/* register TCP socket and UDP socket Read events on */
addfd(epollfd, listenfd);
addfd(epollfd, udpfd);
while (1)
{
int number = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
if (number < 0)
{
printf("epoll failure\n");
break;
}
for (int i = 0; i < number; i++)
{
int sockfd = events[i].data.fd;
if (sockfd == listenfd) /* If it is TCP Listening in socket */
{
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
addfd(epollfd, connfd);
}
else if (sockfd == udpfd) /* If it is UDP Of socket */
{
char buf[UDP_BUFFER_SIZE];
memset(buf, '\0', UDP_BUFFER_SIZE);
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
ret = recvfrom(udpfd, buf, UDP_BUFFER_SIZE, 0, (struct sockaddr*)&client_address, &client_addrlength);
if (ret > 0)
{
sendto(udpfd, buf, UDP_BUFFER_SIZE - 1, 0, (struct sockaddr*)&client_address, client_addrlength);
}
}
else if (events[i].events & EPOLLIN) /* If it is TCP Can write event */
{
char buf[TCP_BUFFER_SIZE];
while (1) /* because TCP The transmission is a byte stream , So we need to use while Make sure all data is read out ,UDP You don't have to use while */
{
memset(buf, '\0', TCP_BUFFER_SIZE);
ret = recv(events[i].data.fd, buf, TCP_BUFFER_SIZE - 1, 0);
if (ret < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK) /* stay Vxorks and Windows On ,EAGAIN be called EWOULDBLOCK */
{
break;
}
close(sockfd);
break;
}
else if (ret == 0)
{
close(sockfd);
}
else
{
send(sockfd, buf, ret, 0);
}
}
}
else
{
printf("something else happened\n");
}
}
}
close(listenfd);
return 0;
}
版权声明
本文为[m0_ fifty-one million five hundred and fifty-one thousand three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231435333737.html
边栏推荐
- 一篇博客让你学会在vscode上编写markdown
- 初始c语言大致框架适合复习和初步认识
- QT interface optimization: QT border removal and form rounding
- Matrix exchange row and column
- Four ways of SSH restricting login
- QT interface optimization: double click effect
- 流程控制之分支语句
- C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】
- 查找水仙花数-for循环实践
- DVWA之暴力破解(Brute Force)Low-->high
猜你喜欢
如何5分钟上手使用OCR
redis的五种数据类型
QT Detailed explanation of pro file
[servlet] detailed explanation of servlet (use + principle)
《JVM系列》 第七章 -- 字节码执行引擎
c语言在结构体传参时参数压栈问题
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
【JZ46 把数字翻译成字符串】
三、梯度下降求解最小θ
C语言知识点精细详解——初识C语言【1】
随机推荐
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
Arduino for esp8266串口功能简介
ArrayList collection basic usage
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
解决computed属性与input的blur事件冲突问题
Achievements in science and Technology (21)
Unity_代码方式添加绑定按钮点击事件
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
MDS55-16-ASEMI整流模块MDS55-16
asp.net使用MailMessage发送邮件的方法
【NLP】HMM隐马尔可夫+维特比分词
【JZ46 把数字翻译成字符串】
本以为能躺着进华为,结果陆续收到京东/滴滴/爱奇艺offer的我迷茫了
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
科技的成就(二十一)
矩阵交换行列
A good tool: aardio
c语言在结构体传参时参数压栈问题
QT actual combat: Yunxi chat room