当前位置:网站首页>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
边栏推荐
- 单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
- 编程哲学——自动加载、依赖注入与控制反转
- Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
- 电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
- MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- 8.4 循环神经网络从零实现
- Nacos uses demo as configuration center (IV)
- 555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
- LotusDB 设计与实现—1 基本概念
猜你喜欢

Branch statement of process control

51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等

机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏

数组模拟队列进阶版本——环形队列(真正意义上的排队)

关于在vs中使用scanf不安全的问题

Nacos uses demo as configuration center (IV)

Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data

C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】

Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)

1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
随机推荐
GIS数据处理-cesium中模型位置设置
select 同时接收普通数据 和 带外数据
【Servlet】Servlet 详解(使用+原理)
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
线程同步、生命周期
8.5 循环神经网络简洁实现
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
自动化的艺术
科技的成就(二十一)
ArrayList集合基本使用
Branch statement of process control
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
电容
Achievements in science and Technology (21)
A good tool: aardio
Detailed explanation of C language P2 selection branch statement
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
A blog allows you to learn how to write markdown on vscode
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
Detailed explanation of SAR command