当前位置:网站首页>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
边栏推荐
- 一个月把字节,腾讯,阿里都面了,写点面经总结……
- C语言p2选择分支语句详解
- 51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- 【JZ46 把数字翻译成字符串】
- Arduino for esp8266串口功能简介
- PCIe X1 插槽的主要用途是什么?
- Detailed explanation of C language P2 selection branch statement
- 查找水仙花数-for循环实践
- DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
- Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
猜你喜欢
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
一篇博客让你学会在vscode上编写markdown
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
ASEMI整流模块MDQ100-16在智能开关电源中的作用
MDS55-16-ASEMI整流模块MDS55-16
电容
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
你还不知道责任链模式的使用场景吗?
随机推荐
ASEMI整流模块MDQ100-16在智能开关电源中的作用
Matrix exchange row and column
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
QT interface optimization: QT border removal and form rounding
自动化的艺术
source insight via samba
MySQL报错packet out of order
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
capacitance
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
C语言知识点精细详解——数据类型和变量【1】——进位计数制
Unity_代码方式添加绑定按钮点击事件
DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
Outsourcing for four years, abandoned
初始c语言大致框架适合复习和初步认识
Nacos uses demo as configuration center (IV)
查找水仙花数-for循环实践
外包干了四年,废了...