当前位置:网站首页>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
边栏推荐
- 【Proteus仿真】自动量程(范围<10V)切换数字电压表
- 单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
- [servlet] detailed explanation of servlet (use + principle)
- 51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- 如何5分钟上手使用OCR
- Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
- SVN详细使用教程
- 关于在vs中使用scanf不安全的问题
- 直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
- 科技的成就(二十一)
猜你喜欢

MySQL报错packet out of order

PCIe X1 插槽的主要用途是什么?

c语言在结构体传参时参数压栈问题

【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序

C语言知识点精细详解——数据类型和变量【1】——进位计数制

AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc

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

阿里研发三面,面试官一套组合拳让我当场懵逼

【工厂模式详解】工厂方法模式

Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
随机推荐
阿里研发三面,面试官一套组合拳让我当场懵逼
2-Go变量操作
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
[servlet] detailed explanation of servlet (use + principle)
电容
epoll 的 ET,LT工作模式———实例程序
Outsourcing for four years, abandoned
QT actual combat: Yunxi calendar
capacitance
Parameter stack pressing problem of C language in structure parameter transmission
8.2 文本预处理
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
【Servlet】Servlet 详解(使用+原理)
MDS55-16-ASEMI整流模块MDS55-16
Logical volume creation and expansion
Solve the problem of SSH configuration file optimization and slow connection