safe close tcp connection

本文通过一个简单的TCP发送端示例代码,讨论了TCP并非绝对可靠的原因。重点解释了send操作成功只意味着数据放入了内核缓冲区,并不表示数据已到达接收端;以及close操作可能带来的问题,比如发送RST而非FIN报文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

 

转载于:https://www.cnblogs.com/hangj/p/6263025.html

162.c:29:5: error: redeclaration of enumerator ‘TCP_LISTEN’ TCP_LISTEN, ^ In file included from 162.c:8:0: /usr/include/netinet/tcp.h:158:3: note: previous definition of ‘TCP_LISTEN’ was here TCP_LISTEN, ^ 162.c:30:5: error: redeclaration of enumerator ‘TCP_SYN_SENT’ TCP_SYN_SENT, ^ In file included from 162.c:8:0: /usr/include/netinet/tcp.h:150:3: note: previous definition of ‘TCP_SYN_SENT’ was here TCP_SYN_SENT, ^ 162.c:32:5: error: redeclaration of enumerator ‘TCP_ESTABLISHED’ TCP_ESTABLISHED, ^ In file included from 162.c:8:0: /usr/include/netinet/tcp.h:149:3: note: previous definition of ‘TCP_ESTABLISHED’ was here TCP_ESTABLISHED = 1, ^ 162.c:35:5: error: redeclaration of enumerator ‘TCP_CLOSE_WAIT’ TCP_CLOSE_WAIT, ^ In file included from 162.c:8:0: /usr/include/netinet/tcp.h:156:3: note: previous definition of ‘TCP_CLOSE_WAIT’ was here TCP_CLOSE_WAIT, ^ 162.c:36:5: error: redeclaration of enumerator ‘TCP_LAST_ACK’ TCP_LAST_ACK, ^ In file included from 162.c:8:0: /usr/include/netinet/tcp.h:157:3: note: previous definition of ‘TCP_LAST_ACK’ was here TCP_LAST_ACK, ^ 162.c:37:5: error: redeclaration of enumerator ‘TCP_CLOSING’ TCP_CLOSING, ^ In file included from 162.c:8:0: /usr/include/netinet/tcp.h:159:3: note: previous definition of ‘TCP_CLOSING’ was here TCP_CLOSING /* now a valid state */ ^ 162.c:38:5: error: redeclaration of enumerator ‘TCP_TIME_WAIT’ TCP_TIME_WAIT, ^ In file included from 162.c:8:0: /usr/include/netinet/tcp.h:154:3: note: previous definition of ‘TCP_TIME_WAIT’ was here TCP_TIME_WAIT, ^ 162.c: In function ‘cleanup_thread’: 162.c:137:9: warning: implicit declaration of function ‘TAILQ_FOREACH_SAFE’ [-Wimplicit-function-declaration] TAILQ_FOREACH_SAFE(conn, &conn_queue, entries, tmp) { ^ 162.c:137:47: error: ‘entries’ undeclared (first use in this function) TAILQ_FOREACH_SAFE(conn, &conn_queue, entries, tmp) { ^ 162.c:137:47: note: each undeclared identifier is reported only once for each function it appears in 162.c:137:61: error: expected ‘;’ before ‘{’ token TAILQ_FOREACH_SAFE(conn, &conn_queue, entries, tmp) { ^ k1@k1:~/work/CDemo$
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值