linux Closing a Socket

SO_LINGER
选项用来指明关闭基于连接的 socket 时的行为。默认,close 函数会立即返回,但当 socket 发送缓冲区残留数据时,close

函数向缓冲区中写入 FIN 后,同样会立即返回,但 TCP 将会尽力发送完这些数据。


When you have finished using a socket, you can simply close its file descriptor with close; see Opening and Closing Files. If there is still data waiting to be transmitted over the connection, normally close tries to complete this transmission. You can control this behavior using the SO_LINGER socket option to specify a timeout period; see Socket Options.

You can also shut down only reception or transmission on a connection by calling shutdown, which is declared in sys/socket.h.

Function:  int  shutdown  (int socket, int how)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

The shutdown function shuts down the connection of socket socket. The argument how specifies what action to perform:

0

Stop receiving data for this socket. If further data arrives, reject it.

1

Stop trying to transmit data from this socket. Discard any data waiting to be sent. Stop looking for acknowledgement of data already sent; don’t retransmit it if it is lost.

2

Stop both reception and transmission.

The return value is 0 on success and -1 on failure. The following errno error conditions are defined for this function:

EBADF

socket is not a valid file descriptor.

ENOTSOCK

socket is not a socket.

ENOTCONN

socket is not connected.


关于tcp_close函数说明

tcp_close()只在用户态调用了close()系统调用后才调用。 如果此时还有数据没有读取,则说明用户是要提前终止连接, 
此时要向对方发RST,因为有对方发送的数据丢失了。 因此将连接直接跳到CLOSE状态,而不是正常的FIN_WAIT_1 


只清除receive queue是因为已经不可能读数据了, 而先前发送的数据可能还在write queue中等待发送。 这里的tcp_close()只是关闭了套接字层, 
tcp协议层仍然在运做。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值