当前位置:网站首页>Select receives both normal data and out of band data
Select receives both normal data and out of band data
2022-04-23 14:40:00 【m0_ fifty-one million five hundred and fifty-one thousand three】
This is a server program , Use select
Receive common data at the same time ( Surveillance is readable ) and Out of band data ( Monitor exceptions ).
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
#include<stdlib.h>
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_family = AF_INET;
address.sin_port = htons(port);
inet_pton(AF_INET, ip, &address.sin_addr);
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);
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
if (connfd < 0)
{
printf("errno is: %d\n", errno);
close(listenfd);
}
char buf[1024];
fd_set read_fds; /* One for testing Readable state File descriptor set */
fd_set exception_fds; /* One for testing Abnormal state of File descriptor set */
FD_ZERO(&read_fds);
FD_ZERO(&exception_fds);
while (1)
{
memset(buf, '\0', sizeof(buf));
/* Every time you call select We have to start again before read_fds and exception_fds Set file descriptor in connfd, Because the set of file descriptors will be modified by the kernel after the event */
FD_SET(connfd, &read_fds);
FD_SET(connfd, &exception_fds);
ret = select(connfd + 1, &read_fds, NULL, &exception_fds, NULL);
if (ret < 0)
{
printf("selection failure\n");
break;
}
/* For readable Events , Use ordinary recv Function to read data */
if (FD_ISSET(connfd, &read_fds))
{
ret = recv(connfd, buf, sizeof(buf) - 1, 0);
if (ret <= 0)
{
break;
}
printf("get %d bytes of normal data: %s\n", ret, buf);
}
/* For exceptional events , Use band MSG_OOB logo recv Function to read out of band data */
else if (FD_ISSET(connfd, &exception_fds))
{
ret = recv(connfd, buf, sizeof(buf) - 1, MSG_OOB);
if (ret <= 0)
{
break;
}
printf("get %d bytes of oob data: %s\n", ret, buf);
}
}
close(connfd);
close(listenfd);
return 0;
}
版权声明
本文为[m0_ fifty-one million five hundred and fifty-one thousand three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231435333839.html
边栏推荐
- Upgrade of openssh and modification of version number
- 【Proteus仿真】自动量程(范围<10V)切换数字电压表
- [detailed explanation of factory mode] factory method mode
- 【JZ46 把数字翻译成字符串】
- 机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
- 解决computed属性与input的blur事件冲突问题
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
- 1-初识Go语言
- Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
- Matrix exchange row and column
猜你喜欢
【NLP】HMM隐马尔可夫+维特比分词
MySQL报错packet out of order
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
UML项目实例——抖音的UML图描述
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
想要成为架构师?夯实基础最重要
ASEMI三相整流桥和单相整流桥的详细对比
随机推荐
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
qt之.pro文件详解
Some little records~
初始c语言大致框架适合复习和初步认识
[servlet] detailed explanation of servlet (use + principle)
Sed learning for application
First acquaintance with STL
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
Unity_代码方式添加绑定按钮点击事件
初识STL
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
[untitled]
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
Qt实战:云曦聊天室篇
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
C语言知识点精细详解——数据类型和变量【1】——进位计数制
Nacos uses demo as configuration center (IV)
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、