当前位置:网站首页>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
边栏推荐
猜你喜欢
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
ASEMI整流模块MDQ100-16在智能开关电源中的作用
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
流程控制之分支语句
Svn detailed use tutorial
全连接层的作用是什么?
[servlet] detailed explanation of servlet (use + principle)
随机推荐
如何打开Win10启动文件夹?
本以为能躺着进华为,结果陆续收到京东/滴滴/爱奇艺offer的我迷茫了
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
JumpServer
ArrayList集合基本使用
【Proteus仿真】自动量程(范围<10V)切换数字电压表
epoll 的 ET,LT工作模式———实例程序
8.3 语言模型与数据集
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
source insight via samba
【无标题】
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
矩阵交换行列
如何5分钟上手使用OCR
capacitance
Usage of BC
帧同步 实现
《JVM系列》 第七章 -- 字节码执行引擎