如何在TCP连接上进行硬/异常关闭? [英] how to make a hard/abortive close on a TCP connection?

查看:195
本文介绍了如何在TCP连接上进行硬/异常关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当TCP客户端与我的TCP服务器建立TCP连接时,它发送几个数据包后,我想对该TCP连接进行硬/异常关闭,如何在Linux C中实现?

when a tcp client establishes a tcp connection with my TCP server, after it sends several packets, I want to make a hard/abortive close on this TCP connection, how to do it in linux C?

此处的硬/非礼拜"关闭意味着tcp服务器将立即向客户端发送RST.完全没有FIN/ACK.

the hard/abortive close here means the tcp server will send a RST immediately to the client. No FIN/ACK at all.

谢谢!

推荐答案

来自套接字手册页

SO_LINGER 设置或获取SO_LINGER选项.该论点是一个持久的结构.

SO_LINGER Sets or gets the SO_LINGER option. The argument is a linger structure.

struct linger {
    int l_onoff;    /* linger active */
    int l_linger;   /* how many seconds to linger for */
};

启用后,close(2)或shutdown(2)直到所有排队的消息都不会返回 表示已成功发送套接字,或已达到延迟超时.否则,该调用将立即返回,并在后台完成关闭.当套接字作为exit(2)的一部分关闭时,它始终在后台徘徊.

When enabled, a close(2) or shutdown(2) will not return until all queued messages for the socket have been successfully sent or the linger timeout has been reached. Otherwise, the call returns immediately and the closing is done in the background. When the socket is closed as part of exit(2), it always lingers in the background.

其他设置:

setsockopt(...,SO_LINGER,...)的效果取决于延迟结构(传递给setsockopt()的第三个参数)中的值是什么:

The effect of an setsockopt(..., SO_LINGER,...) depends on what the values in the linger structure (the third parameter passed to setsockopt()) are:

案例1:linger-> l_onoff为零(linger-> l_linger没有意义): 这是默认设置.

Case 1: linger->l_onoff is zero (linger->l_linger has no meaning): This is the default.

在close()上,在确保发送所有未发送的数据之后,基础堆栈尝试正常关闭连接.对于TCP等面向连接的协议,堆栈还可以确保对等方确认发送的数据.无论套接字是阻塞还是非阻塞,堆栈都会在后台执行上述正常关机(在对close()的调用返回之后).

On close(), the underlying stack attempts to gracefully shutdown the connection after ensuring all unsent data is sent. In the case of connection-oriented protocols such as TCP, the stack also ensures that sent data is acknowledged by the peer. The stack will perform the above-mentioned graceful shutdown in the background (after the call to close() returns), regardless of whether the socket is blocking or non-blocking.

案例2:linger-> l_onoff不为零,而linger-> l_linger为零:

Case 2: linger->l_onoff is non-zero and linger->l_linger is zero:

close()立即返回.基础堆栈丢弃所有未发送的数据,并且在面向连接的协议(例如TCP)的情况下,将RST(重置)发送到对等方(这称为硬关闭或异常关闭).对等方的应用程序随后进行的所有尝试读取()/recv()数据的操作都会导致ECONNRESET.

A close() returns immediately. The underlying stack discards any unsent data, and, in the case of connection-oriented protocols such as TCP, sends a RST (reset) to the peer (this is termed a hard or abortive close). All subsequent attempts by the peer's application to read()/recv() data will result in an ECONNRESET.

案例3:linger-> l_onoff不为零,而linger-> l_linger不为零:

Case 3: linger->l_onoff is non-zero and linger->l_linger is non-zero:

close()将阻塞(如果是阻塞套接字)或失败,并以EWOULDBLOCK失败(如果非阻塞),直到正常关闭完成或linger-> l_linger中指定的时间过去(超时)为止.超时后,堆栈的行为与上述情况2相同.

A close() will either block (if a blocking socket) or fail with EWOULDBLOCK (if non-blocking) until a graceful shutdown completes or the time specified in linger->l_linger elapses (time-out). Upon time-out the stack behaves as in case 2 above.

这篇关于如何在TCP连接上进行硬/异常关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆