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 并不是绝对可靠的

safe close tcp connection的更多相关文章

  1. TCP connection status

    A TCP connection progresses through a series of states during its lifetime. The following diagram il ...

  2. linux上TCP connection timeout的原因查找

    linux上TCP connection timeout的原因查找 好久没有写文章了, 今天解决了一个网络连接超时的问题, 记录以备查看. 最近在线上nginx经常出现输出connection tim ...

  3. TCP Connection Establishment and Termination

    Three-Way Handshake The following scenario occurs when a TCP connection is established: The server m ...

  4. RT:How HTTP use TCP connection

    In HTTP/0.9 (not used anymore), each request uses a separate TCP connection, and the end of a respon ...

  5. RabbitMQ问题解决:TCP connection succeeded but Erlang distribution failed

    说明 本来是要先把Hystrix 仪表盘更完的,但是出现了Turbine.Dashboard.RabbitMQ整合实现监控. 所以先在学RabbitMq的基本操作,在安装过程中出现了 E:\Rabbi ...

  6. tcp connection

    三次握手与四次挥手的原因 https://yq.aliyun.com/articles/7435?spm=5176.8091938.0.0.N4v33a linux里的backlog详解 tcp co ...

  7. Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space

    在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" ...

  8. the age of the TCP connection TCP Slow Start

    w防止网络过载和拥塞 HTTP The Definitive Guide The performance of TCP data transfer also depends on the age of ...

  9. A TCP connection is distinguished by four values

    4个值唯一地定义一条TCP连接. HTTP The Definitive Guide A computer might have several TCP connections open at any ...

随机推荐

  1. ServiceDesk Plus解析内容,简化工单管理

  2. 762. Prime Number of Set Bits in Binary Representation

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  3. Linux关机操作

    正确的关机流程为:sync > shutdown > reboot > halt 关机指令为:shutdown ,你可以man shutdown 来看一下帮助文档. 例如你可以运行如 ...

  4. Linux 目录说解

    目录 1.树状目录结构图 2./目录 3./etc/目录 4./usr/目录 5./var/目录 6./proc/目录 7./dev/目录 该文章主要来自于网络进行整理. 目录结构参考地址: http ...

  5. Java对象的克隆

    今天要介绍一个概念,对象的克隆.本篇有一定难度,请先做好心理准备.看不懂的话可以多看两遍,还是不懂的话,可以在下方留言,我会看情况进行修改和补充. 克隆,自然就是将对象重新复制一份,那为什么要用克隆呢 ...

  6. 以太网MAC地址规范

    原文地址:http://blog.csdn.net/skyflying2012/article/details/40322563 之前一段时间在做网卡驱动的工作,现在产品量产,利用ifconfig e ...

  7. GCC基础知识学习

    GCC基础知识学习 一.GCC编译选项解析 常用编译选项 命令格式:gcc [选项] [文件名] -E:仅执行编译预处理: -S:将C代码转换为汇编代码: -c:仅执行编译操作,不进行连接操作: -o ...

  8. Vue上传文件:ElementUI中的upload实现

    一.上传文件实现 两种实现方式: 1.直接action <el-upload  .利用before-upload属性 此种方式有个弊端,就是action是必选的参数,那么action如果和pos ...

  9. linux上搭建solr(用tomcat部署)

    环境centos7 及最新版本solr7 1.先在/usr/local下mkdir solr 2.在里面新建一个tomcat目录 3.拷贝已经解压的tomat到/usr/local/solr/tomc ...

  10. Java应用分类

    Java应用分类     一.应用程序.指在操作系统上直接运行的,不是浏览器,Java环境用本机的,需要在客户端安装,Java环境可以一起安装.         1.GUI图形界面应用程序       ...