如何在FTP服务器上对EPSV(ALL)命令作出反应?

时间:2019-04-25 19:59:41

标签: java sockets ftp

我正在尝试通过Wi-Fi在本地网络上以Java设置FTP服务器。而且我坚持处理EPSV ALL命令。作为客户端,我在iPhone上使用VLC播放器。当我尝试播放.mp4文件时,它正在向服务器发送EPSV ALL。服务器响应如下:

"229 Entering Extended Passive Mode (|||" + freeDataPort + "|)"

服务器在serverSocket上创建freeDataPort,侦听收入连接,但没有任何反应。

我已经尝试过打开与FileZilla服务器连接的电影-它可以工作。我也不明白为什么客户端尝试在此命令(EPSV之后建立第二个连接(请求用户并再次通过的连接),而PASV命令创建数据连接并进行简单的数据传输,例如响应到LIST,在第一个中处理。

这是我使用的EPSV的处理程序:

private void handleEpsv() {
    sendMsgToClient("229 Entering Extended Passive Mode (|||" + freeDataPort + "|)");
    try {
        dataSocket = new ServerSocket(freeDataPort);
        System.out.println("waiting for connect... port: " + freeDataPort);
        dataConnection = dataSocket.accept();
        dataOutWriter = new PrintWriter(dataConnection.getOutputStream(), true);
    } catch (IOException e)
    {
        debugOutput("Could not create data connection.");
        e.printStackTrace();
    }
}

最后一次得到的结果:

FTP Server started listening on port 21
{/192.168.0.105=INITIALThread-0    Threads in data list: 0}
New connection received. Worker was created.
from INITIAL THREAD
Thread-0 - send to client:    220 Welcome to the FTP-Server

USER a from INITIAL THREAD
Thread-0 - send to client:    331 User name okay, need password

PASS a from INITIAL THREAD
Thread-0 - send to client:    230 User logged in successfully

SYST from INITIAL THREAD
Thread-0 - send to client:    215 UNIX Type: L8

PWD from INITIAL THREAD
Thread-0 - send to client:    257 "/"

TYPE I from INITIAL THREAD
Thread-0 - send to client:    200 OK

CWD // from INITIAL THREAD
Thread-0 - send to client:    250 CWD successful. / is current directory

PASV from INITIAL THREAD
Thread-0 - send to client:    227 Entering Passive Mode (192,168,0,199,7,232)

waiting for connect... port: 2024
Data connection - Passive Mode - established
LIST from INITIAL THREAD
Thread-0 - send to client:    125 Opening ASCII mode data connection for file list.

from INITIAL THREAD
Thread-0 - send to client:    226 Transfer complete.

{/192.168.0.105=INITIALThread-0    Threads in data list: 1}
New connection received. Worker was created.

Thread-1 - send to client:    220 Welcome to the FTP-Server

FEAT 
Thread-1 - send to client:    211-Features:


Thread-1 - send to client:    MDTM


Thread-1 - send to client:    REST STREAM


Thread-1 - send to client:    SIZE


Thread-1 - send to client:    MLST type*;size*;modify*;


Thread-1 - send to client:    MLSD


Thread-1 - send to client:    UTF8


Thread-1 - send to client:    CLNT


Thread-1 - send to client:    MFMT


Thread-1 - send to client:    EPSV


Thread-1 - send to client:    EPRT


Thread-1 - send to client:    211 END

USER a 
Thread-1 - send to client:    331 User name okay, need password

PASS a 
Thread-1 - send to client:    230 User logged in successfully

EPSV ALL 
Thread-1 - send to client:    229 Entering Extended Passive Mode (|||2025|)

used by Thread-1 Thread DATA type
waiting for connect... port: 2025

1 个答案:

答案 0 :(得分:1)

EPSV ALL不是EPSVEPSV ALL具有特殊含义。客户端使用它来指示它将仅使用EPSV,而不使用PASVPORTEPRT。您只应对此回应200 OK。您应该阅读RFC 2428

实际上,我从未见过使用此命令的任何FTP客户端。大多数服务器将忽略它或不正确地处理它。 VLC无法与服务器通信的原因可能是,除了响应不正确之外,您还阻塞了控件的连接以等待传入的数据传输。而且VLC正在发送更多命令,并超时等待服务器响应(因此,它可能会通过打开新连接重试)。

这是VLC与FileZilla FTP服务器连接的记录。请注意,FileZilla还通过响应EPSV ALL来错误地处理229 Entering Extended Passive Mode。但这不会阻止控件连接,并且可以正确处理以下命令(包括以后的EPSV)。

230 Logged on
EPSV ALL
229 Entering Extended Passive Mode (|||65079|)
TYPE I
200 Type set to I
SIZE video.avi
213 119035510
EPSV
229 Entering Extended Passive Mode (|||63465|)
TYPE I
200 Type set to I
RETR video.avi
150 Opening data channel for file download from server of "/video.avi"
...

实际上,即使命令是正常的EPSV,也不应阻止控制连接,因为客户端将发送其他命令,尤其是RETR(或STOR,{ {1}},LIST ...)。