当前位置:网站首页>SOCKET【5】- struct linger 用法
SOCKET【5】- struct linger 用法
2022-06-11 07:44:00 【大城市的小蜗牛】
前言
Linux下tcp链接断开的时候调用close()函数,有优雅的断开和强制断开两种方式。
一、如何设置断开连接的方式呢
通过设置socket描述符一个linger结构体属性linger结构体数据结构如下:
#include <arpa/inet.h>
struct linger{
int l_onoff;
int l_linger;
};
二、三种断开方式
l_onoff=0;l_linger忽略close立即返回,底层会将未发送完的数据发送完后再释放资源,即优雅的退出。l_onoff !=0;l_linger = 0close()立刻返回,但不会发送未发送完的数据,而是通过REST包强制关闭socket描述符,即强制退出l_onoff !=0;l_linger > 0close不会立刻返回,内核会延时一段时间,这个时间就由l_linger的值来决定。如果超时时间到达之前,发送完未发送的数据(包括FIN包)并得到另一端的确认,close()会返回正确,socket描述符优雅性退出。否则,close()会直接返回错误值,未发送数据丢失,socket描述符被强制性退出。需要注意的是,如果socket描述符被设置未非阻塞型,则close()会直接返回值。
三、具体用法
struct linger ling = {
0, 0};
setsockopt(socketfd, SOL_SOCKET, SO_LINGER, (void*)&ling, sizeof(ling));
边栏推荐
- What exactly is PMP?
- El expressions and JSTL
- 134. 加油站
- 排序——归并排序
- 远程办公经验分享 | 社区征文
- 零基础自学SQL课程 | OUTER JOIN外连接
- Detailed explanation of character function and string function (including simulation implementation)
- Configuration software -- control drag and drop
- MFC auxiliary CString string splicing
- The solution of "no startup device" after running Bochs
猜你喜欢
![20200727 T2 small w playing game [generating function (binomial inversion technique)]](/img/a5/ae2192f4f37232cdcb01e81ad0297c.jpg)
20200727 T2 small w playing game [generating function (binomial inversion technique)]

Use of wordcloud

Tidb Cloud est en ligne sur le marché Google Cloud pour permettre aux développeurs du monde entier d'utiliser une nouvelle pile de bases de données htap en temps réel

Servlet
![[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products](/img/85/5766d8269391820b5e142178530657.png)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products

Implementation of stack (C language)

RTMP protocol

QT picture adaptive display control

C language - Growth Diary -01- count primes and sum

About static keyword
随机推荐
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
C language Yanghui triangle code
C language inherits memory management mechanism (unfinished)
Qstring to hexadecimal qstring
C language function stack frame
Introduction to operations research
Qunhui ds918 creates m.2 SSD read / write cache
C- print 99 multiplication table
Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本
【CodeForces908H】New Year and Boolean Bridges (FWT)
Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...
Nim product
C language - growth diary-04- preliminary exploration of local variables (local variables)
[atcoder1981] short diameter (graph theory thinking)
【AtCoder1998】Stamp Rally(整体二分+并查集)
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
multi-sig SC
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
