当前位置:网站首页>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
边栏推荐
- 成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
- 关于在vs中使用scanf不安全的问题
- LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
- Qt实战:云曦聊天室篇
- 电容
- 【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
- 四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
- L'externalisation a duré quatre ans.
- QT interface optimization: double click effect
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
猜你喜欢
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
抑郁症治疗的进展
555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
[detailed explanation of factory mode] factory method mode
[servlet] detailed explanation of servlet (use + principle)
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
capacitance
PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)
外包幹了四年,廢了...
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】
随机推荐
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
Detailed explanation of SAR command
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
QT interface optimization: QT border removal and form rounding
Want to be an architect? Tamping the foundation is the most important
拼接hql时,新增字段没有出现在构造方法中
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
Logical volume creation and expansion
51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
你還不知道責任鏈模式的使用場景嗎?
想要成为架构师?夯实基础最重要
外包干了四年,废了...
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
帧同步 实现
Qt界面优化:Qt去边框与窗体圆角化
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)