在 TCP/IP 套接字连接中发送重置 [英] Sending a reset in TCP/IP Socket connection
问题描述
我正在使用 python 的 socket.py 创建到 ftp 服务器的连接.现在我想重置连接(发送一个 RST 标志)并监听 ftp-server 的响应.(仅供参考,使用 socket.send('','R') 不起作用,因为操作系统发送的是 FIN 标志而不是 RST.)
I am using python’s socket.py to create a connection to an ftp-server. Now I want to reset the connection (send a RST Flag) and listen to the response of the ftp-server. (FYI using socket.send('','R') does not work as the OS sends FIN flag instead of RST.)
推荐答案
打开 SO_LINGER 套接字选项并将延迟时间设置为 0 秒.这将导致 TCP 在关闭时中止连接,刷新数据并发送 RST.参见 UNP 中的第 7.5 节和示例 15.21.
Turn the SO_LINGER socket option on and set the linger time to 0 seconds. This will cause TCP to abort the connection when it is closed, flush the data and send a RST. See section 7.5 and example 15.21 in UNP.
在蟒蛇中:
def client(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.connect((host, port))
l_onoff = 1
l_linger = 0
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', l_onoff, l_linger))
# send data here
s.close()
这篇关于在 TCP/IP 套接字连接中发送重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!