当前位置:网站首页>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));
边栏推荐
- 模线性方程组(中国剩余定理+通用解法)
- The solution of "no startup device" after running Bochs
- Note: JDBC
- 【CodeForces1019E】Raining season(边分治+斜率优化)
- multi-sig SC
- 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
- After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
- 【AtCoder2000】Leftmost Ball (DP+组合数)
- [atcoder1984] wide swap
- 【IoT】项目管理:如何打造更好的跨职能团队?
猜你喜欢

黑群晖DSM7.0.1物理机安装教程

Zero foundation self-study SQL course | union joint query

C language to write a calculator calculation logic
![[IOT] project management: how to build a better cross functional team?](/img/df/28dbf0f7ba75d1bb3469cc15e70538.png)
[IOT] project management: how to build a better cross functional team?

【IoT】项目管理:如何打造更好的跨职能团队?

Simple configuration of vscade

Tidb cloud launched Google cloud marketplace, empowering global developers with a new stack of real-time HTAP databases

Miscellany C language

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

QObject usage skills -- control function class
随机推荐
【AtCoder2387】+/- Rectangle
欧拉定理及扩展(附证明)
【AtCoder2304】Cleaning
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
【HDU6357】Hills And Valleys(DP)
Arduino_ STM development record
远程办公经验 | 社区征文
pmp到底是什么?
String Simulation Implementation
860. 柠檬水找零
Classes and objects (Part 2)
批量拼接字符串
Second remaining learning notes
Rabin-Miller素数测试
【IoT】项目管理:如何打造更好的跨职能团队?
Djikstra solves the shortest circuit with negative weight
C language lesson 2
C language to achieve three piece chess (not artificial mental retardation ha ha ha)
Miscellany C language
[IOT] project management: how to build a better cross functional team?
