在TCP/IP套接字连接中发送重置

在TCP/IP套接字连接中发送重置

问题描述:

我正在使用python的socket.py创建与ftp服务器的连接.现在,我想重置连接(发送RST标志)并收听ftp服务器的响应. (FYI使用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.请参阅第7.5节和UNP中的示例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.

在python中:

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()