当前位置:网站首页>One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
2022-04-23 14:40:00 【m0_ fifty-one million five hundred and fifty-one thousand three】
One 、 What is Non blocking connect
?
The only non blocking we've used before socket The file descriptor is in Server , Using non blocking methods can greatly improve the efficiency of server programs .
Now imagine , We put Client program socket Set to non blocking , So what should I do socket File descriptors connect
The connection is easy to report errors . Why? ? because , Non blocking , The call to the file descriptor directly returns the result , and socket It's hard to connect so fast , So it's easy to call and fail , This will set errno
The value of is :EINPROGRESS
. This error type occurs in , For non blocking socket call connect
, When the connection is not established . So when connect
return -1 when , It doesn't have to be a mistake , Maybe the connection is still in progress !
The solution to this matter is , call select
、poll
Wait for the function to listen for the connection failure socket Writable events on . When select
or poll
When the function returns , recycling getsockopt
Read the error code . If the error code is 0, Just now connect There was no mistake , Connection established successfully , If the error code is 1, Indicates that an error has indeed occurred .
So troublesome to use non blocking connect
Is there any benefit ?
By using non blocking connect
, We will Can initiate multiple connections at the same time and wait together .
2、 Non blocking connect
Example program
The following is a client program , It uses a non blocking socket Connect to server .
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<time.h>
#include<sys/ioctl.h>
#define BUFFER_SIZE 1023
int setnonblocking(int fd)
{
int old_option = fcntl(fd, F_GETFL);
int new_option = old_option | O_NONBLOCK;
fcntl(fd, F_SETFL, new_option);
return old_option;
}
/* Timeout connection function , The parameters are IP Address , Port number , And overtime ( millisecond ). Successful return of connection status socket, Failure to return -1 */
int unblock_connect(const char* ip, int port, int time)
{
int ret = 0;
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_pton(AF_INET, ip, &address);
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
int fdopt = setnonblocking(sockfd);
ret = connect(sockfd, (struct sockaddr*)&address, sizeof(address));
if (ret == 0) /* The connection is successful */
{
/* If the connection is successful , Then recover sockfd Properties of , And return immediately */
printf("connect whit server immediately\n");
fcntl(sockfd, F_SETFL, fdopt);
return sockfd;
}
else if (errno != EINPROGRESS) /* If the error type is not EINPROGRESS, Indicates that other errors have occurred , Return immediately */
{
/* If the connection is not established immediately , Then only when errno yes EINPROGRESS The connection is still in progress , Otherwise, an error is returned */
printf("unblock connect not support\n");
return -1;
}
fd_set readfds;
fd_set writefds;
struct timeval timeout;
FD_ZERO(&readfds);
FD_SET(sockfd, &writefds);
timeout.tv_sec = time;
timeout.tv_usec = 0;
ret = select(sockfd + 1, NULL, &writefds, NULL, &timeout);
if (ret <= 0)
{
/* select Timeout or error , Return immediately */
printf("connection time out\n");
close(sockfd);
return -1;
}
if (!FD_ISSET(sockfd, &writefds)) /* If sockfd Don't write , Indicates that the connection failed , Return immediately */
{
printf("no events on sockfd found\n");
close(sockfd);
return -1;
}
int error = 0;
socklen_t length = sizeof(error);
/* call getsockopt To get and clear sockfd Mistakes on */
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &length) < 0)
{
printf("connection failed after select with the error: %d\n", error);
close(sockfd);
return -1;
}
/* Error number is not 0 Indicates a connection error */
if (error != 0)
{
printf("connection failed after select with the error: %d\n", error);
close(sockfd);
return -1;
}
/* Successful connection */
printf("connection ready after select with the socket: %d\n", sockfd);
fcntl(sockfd, F_SETFL, fdopt);
return sockfd;
}
int main(int argc, char* argv[])
{
if (argc <= 2)
{
return 1;
}
const char* ip = argv[1];
int port = atoi(argv[2]);
int sockfd = unblock_connect(ip, port, 10);
if (sockfd < 0)
{
return 1;
}
close(sockfd);
return 0;
}
版权声明
本文为[m0_ fifty-one million five hundred and fifty-one thousand three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231435333788.html
边栏推荐
- [untitled]
- [jz46 translate numbers into strings]
- 数组模拟队列进阶版本——环形队列(真正意义上的排队)
- 8.3 语言模型与数据集
- C语言知识点精细详解——数据类型和变量【1】——进位计数制
- L'externalisation a duré quatre ans.
- The initial C language framework is suitable for review and preliminary understanding
- 如何5分钟上手使用OCR
- 《JVM系列》 第七章 -- 字节码执行引擎
- Find daffodils - for loop practice
猜你喜欢
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
【工厂模式详解】工厂方法模式
1-初识Go语言
QT interface optimization: QT border removal and form rounding
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
Parameter stack pressing problem of C language in structure parameter transmission
随机推荐
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
The art of automation
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
On the insecurity of using scanf in VS
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
循环队列的基本操作,你学会了吗?
Qt实战:云曦日历篇
2-Go变量操作
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
抑郁症治疗的进展
asp.net使用MailMessage发送邮件的方法
【Servlet】Servlet 详解(使用+原理)
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
初识STL
Matrix exchange row and column
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
编程哲学——自动加载、依赖注入与控制反转
QT actual combat: Yunxi calendar
Model location setting in GIS data processing -cesium
流程控制之分支语句