当前位置:网站首页>Socket [5] - struct linker usage

Socket [5] - struct linker usage

2022-06-11 07:49:00 A small snail in a big city


Preface

Linux Next tcp Call when the link is broken close() function , There are two ways of graceful disconnection and forced disconnection .


One 、 How to set the way to disconnect

By setting socket A descriptor linger Structure properties
linger The structure data structure is as follows :

#include <arpa/inet.h>
struct linger{
    
	int l_onoff;
	int l_linger;
};

Two 、 Three disconnection modes

  1. l_onoff=0;l_linger Ignore
    close Return immediately , The bottom layer will release resources after sending the unsent data , namely Exit gracefully .
  2. l_onoff !=0;l_linger = 0
    close() Go back to , But it will not send unfinished data , But through REST The package is forced to close socket The descriptor , namely Forced exit
  3. l_onoff !=0;l_linger > 0
    close Not immediately back , The kernel will delay for some time , This time is determined by l_linger The value of . If the time-out period expires , Sending unsent data ( Include FIN package ) And get confirmation from the other end ,close() Will return correct ,socket The descriptor Elegance sign out . otherwise ,close() The error value will be returned directly , Unsent data loss ,socket Descriptor is forced to exit . It should be noted that , If socket Descriptor is not set to non blocking , be close() Will directly return the value .

3、 ... and 、 The specific use

struct linger ling = {
    0, 0};
setsockopt(socketfd, SOL_SOCKET, SO_LINGER, (void*)&ling, sizeof(ling));

 Insert picture description here
Network programming 4-socket Communication three handshakes and four waves

原网站

版权声明
本文为[A small snail in a big city]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110743536294.html