当前位置:网站首页>select 同时接收普通数据 和 带外数据
select 同时接收普通数据 和 带外数据
2022-04-23 14:36:00 【m0_51551385】
这是一个服务器程序,使用select
同时接收普通数据(监视可读) 和 带外数据(监视异常)。
#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; /* 一个用于测试 可读状态的 文件描述符集合 */
fd_set exception_fds; /* 一个用于测试 异常状态的 文件描述符集合 */
FD_ZERO(&read_fds);
FD_ZERO(&exception_fds);
while (1)
{
memset(buf, '\0', sizeof(buf));
/* 每次调用 select 前都要重新在 read_fds 和 exception_fds 中设置文件描述符 connfd,因为事件发生之后文件描述符集合将被内核修改 */
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;
}
/* 对于可读事件,采用普通的 recv 函数读取数据 */
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);
}
/* 对于异常事件,采用带 MSG_OOB 标志的 recv 函数读取带外数据 */
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_51551385]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_51551385/article/details/124362246
边栏推荐
- OpenFaaS实战之四:模板操作(template)
- 初识STL
- source insight via samba
- Qt界面优化:Qt去边框与窗体圆角化
- MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
- 8.2 文本预处理
- TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
- 51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
- C语言p2选择分支语句详解
- TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
猜你喜欢
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
OpenFaaS实战之四:模板操作(template)
8.4 循环神经网络从零实现
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
详解TCP的三次握手
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
SVN详细使用教程
外包干了四年,废了...
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
随机推荐
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
8.4 循环神经网络从零实现
Solve the problem of SSH configuration file optimization and slow connection
浅谈skiplist在LevelDB的应用
First acquaintance with STL
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
Chapter 7 of JVM series -- bytecode execution engine
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
【Servlet】Servlet 详解(使用+原理)
Detailed explanation of SAR command
Branch statement of process control
Detailed explanation of C language P2 selection branch statement
UML项目实例——抖音的UML图描述
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
拼接hql时,新增字段没有出现在构造方法中
JumpServer
Qt实战:云曦聊天室篇
线程同步、生命周期
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示