当前位置:网站首页>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
边栏推荐
- GIS数据处理-cesium中模型位置设置
- 你还不知道责任链模式的使用场景吗?
- SVN详细使用教程
- Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
- c语言在结构体传参时参数压栈问题
- MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
- 四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
- [jz46 translate numbers into strings]
- 555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
- 关于在vs中使用scanf不安全的问题
猜你喜欢

金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)

QT actual combat: Yunxi chat room

GIS数据处理-cesium中模型位置设置

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

OpenFaaS实战之四:模板操作(template)

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

AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等

I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another

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

Find daffodils - for loop practice
随机推荐
qt之.pro文件详解
Nacos uses demo as configuration center (IV)
【Proteus仿真】自动量程(范围<10V)切换数字电压表
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
一篇博客让你学会在vscode上编写markdown
阿里研发三面,面试官一套组合拳让我当场懵逼
c语言在结构体传参时参数压栈问题
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
Qt界面优化:鼠标双击特效
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
初始c语言大致框架适合复习和初步认识
8.3 语言模型与数据集
Solve the problem of SSH configuration file optimization and slow connection
Four ways of SSH restricting login
Qt界面优化:Qt去边框与窗体圆角化
epoll 的 ET,LT工作模式———实例程序