当前位置:网站首页>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
边栏推荐
- source insight via samba
- asp.net使用MailMessage发送邮件的方法
- 【Proteus仿真】自动量程(范围<10V)切换数字电压表
- Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
- [servlet] detailed explanation of servlet (use + principle)
- 一篇博客让你学会在vscode上编写markdown
- ArrayList collection basic usage
- Parameter stack pressing problem of C language in structure parameter transmission
- 外包幹了四年,廢了...
- 四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
猜你喜欢
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
【NLP】HMM隐马尔可夫+维特比分词
爬虫练习题(一)
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
Outsourcing for four years, abandoned
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
随机推荐
流程控制之分支语句
The initial C language framework is suitable for review and preliminary understanding
Upgrade of openssh and modification of version number
Redis源码分析之HSET流程与ziplist
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
redis 模块编程中 key value的生命周期
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
C语言p2选择分支语句详解
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
线程同步、生命周期
TUN 设备原理
全连接层的作用是什么?
一款不错的工具:aardio
Some little records~
GIS数据处理-cesium中模型位置设置
Docker (V) MySQL installation
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示