hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - 文章 - 后端开发 - 正文 看Cosplay古风插画小姐姐,合集图集打包下载:炫龙网 · 炫龙图库

golang推断tcp是不是断开的要领_后端开发

2019-12-18后端开发ki4网21°c
A+ A-

golang推断tcp是不是断开的要领:

keepalive检测,关于设置了keepalive来讲,当tcp检测到对端socket不再可用时(不能发出探测包,或探测包没有收到ACK的相应包),select会返回socket可读,而且在recv时返回-1,同时置上errno为ETIMEDOUT。此时TCP的状况是断开的。

struct TCP_KEEPALIVE {  
    unsigned long onoff;  
    unsigned long keepalivetime;  
    unsigned long keepaliveinterval;  
} ;  
  
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)  
  
/* KeepAlive完成 */  
void set_keepalive (SOCKET s)  
{  
    BOOL bKeepAlive = TRUE;  
    int nRet = ::setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char*)&bKeepAlive, sizeof(bKeepAlive));  
    if (nRet == SOCKET_ERROR)  
    {  
        return ;  
    }  
    /* 输入参数 */  
    struct TCP_KEEPALIVE inKeepAlive = {0};   
    unsigned long ulInLen = sizeof(struct TCP_KEEPALIVE);  
  
    /* 输出参数 */  
    struct TCP_KEEPALIVE outKeepAlive = {0};   
    unsigned long ulOutLen = sizeof(struct TCP_KEEPALIVE);  
    unsigned long ulBytesReturn = 0;  
    int ret = 0;  
  
    /* 设置keepalive 为5秒,而且发送次数为3次 */  
    inKeepAlive.onoff             = 1;  
    /* 2次keepalive探测间隔时候 */  
    inKeepAlive.keepaliveinterval = 5000;   
    /* 入手下手初次keepalive探测前的tcp余暇时候 */  
    inKeepAlive.keepalivetime     = 5000;   
  
    ret = WSAIoctl((unsigned int)s,  
            SIO_KEEPALIVE_VALS,  
            (LPVOID)&inKeepAlive,   
            ulInLen,  
            (LPVOID)&outKeepAlive,   
            ulOutLen,  
            &ulBytesReturn,   
            NULL,   
            NULL);  
    if (ret == SOCKET_ERROR)  
    {  
        printf ("error: %d\n", WSAGetLastError());  
    }  
}

更多golang学问请关注golang教程栏目。

以上就是golang推断tcp是不是断开的要领的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
golang推断tcp是不是断开的要领_后端开发

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签:

发表评论

选填

必填

必填

选填

请拖动滑块解锁
>>