safe close tcp connection

https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable

http://stackoverflow.com/questions/8874021/close-socket-directly-after-send-unsafe

发送端代码

    sock = socket(AF_INET, SOCK_STREAM, 0);
    connect(sock, &remote, sizeof(remote));
    write(sock, buffer, 1000000);             // returns 1000000
    shutdown(sock, SHUT_WR);  // 最后这一步要加个超时,防止接收端恶意发送数据
    for(;;) {
        res=read(sock, buffer, 4000);
        if(res < 0) {
            perror("reading");
            exit(1);
        }
        if(!res)
            break;
    }
    close(sock);

这么做的原因是

1. send 成功只是把数据放到了内核的发送缓冲区

2. 当调用 close 时,如果 TCP 的接收缓冲区内有数据,会导致发送 RST(而不是 FIN),并清空发送缓冲区。

最好接收端在应用层返回 ack 给发送端。TCP 并不是绝对可靠的

时间: 2024-10-26 07:28:40

safe close tcp connection的相关文章

idea启动tomcat服务失败 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild:

我的工程是从eclipse生成的,个人习惯用idea开发.重复了一遍以往正常的不能再正常了的导入配置,结果遇到了如下问题: SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start comp

A TCP connection is distinguished by four values

4个值唯一地定义一条TCP连接. HTTP The Definitive Guide A computer might have several TCP connections open at any one time. TCP keeps all these connections straight through port numbers. Port numbers are like employees' phone extensions. Just as a company's main

HttpWebRequest&#39;s Timeout and ReadWriteTimeout — What do these mean for the underlying TCP connection?

http://stackoverflow.com/questions/7250983/httpwebrequests-timeout-and-readwritetimeout-what-do-these-mean-for-the-unde https://support.microsoft.com/en-us/kb/904262 https://msdn.microsoft.com/library/e1ax3cw0.aspx?f=255&MSPPError=-2147217396 https:/

ASA TCP Connection Flags

Have you ever wondered what the flags meant when you issued the show conn or show connections command? This post will demystify that for you. Viewing the Connections Use the command show conn to view the connections currently going through the firewa

Exception in thread &quot;RMI TCP Connection(idle)&quot; java.lang.OutOfMemoryError: PermGen space

在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space 在google半天后,找到解决方法: Tomcat直接运行 Catalina.sh/Catalina.bat 设置: windows: set JAVA_OPTS="-Xms1024m -Xmx10246m -X

TCP Connection Management(1)

Introduction Recall that TCP's service model is a byte stream. TCP detects and repairs essentially all the data transfer problems that may be introduced by packet loss, duplication, or errors at the IP layer (or below). UDP is a connectionless protoc

TCP Connection Management(4)

Reset Segments A segment hav- ing this bit set to "on" is called a "reset segment" or simply a "reset." In general, a reset is sent by TCP whenever a segment arrives that does not appear to be correct for the referenced conne

RT: TCP connection close

CLOSE is an operation meaning "I have no more data to send." The notion of closing a full-duplex connection is subject to ambiguous interpretation, of course, since it may not be obvious how to treat the receiving side of the connection. We have

TCP Connection Management(2)

Connections and Translators NAT TCP Options Every option begins with a 1-byte kind that specifies the type of option. Options that are not understood are simply ignored, according to [RFC1122]. The options with a kind value of 0 and 1 occupy a single