当前位置:网站首页>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
边栏推荐
- Upgrade of openssh and modification of version number
- Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
- C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
- 机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
- AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
- 1-初识Go语言
- source insight via samba
- Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
- LotusDB 设计与实现—1 基本概念
- 八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
猜你喜欢
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
自动化的艺术
[untitled]
ASEMI超快恢复二极管与肖特基二极管可以互换吗
一款不错的工具:aardio
555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
你還不知道責任鏈模式的使用場景嗎?
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
qt之.pro文件详解
数组模拟队列进阶版本——环形队列(真正意义上的排队)
随机推荐
First acquaintance with STL
ArrayList collection basic usage
ArrayList集合基本使用
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
Use of ansible and common modules
Branch statement of process control
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
阿里研发三面,面试官一套组合拳让我当场懵逼
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
【工厂模式详解】工厂方法模式
GIS数据处理-cesium中模型位置设置
Four ways of SSH restricting login
Don't you know the usage scenario of the responsibility chain model?
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
Master in minutes --- ternary operator (ternary operator)
PCIe X1 插槽的主要用途是什么?
8.4 循环神经网络从零实现
Nacos uses demo as configuration center (IV)