3

I have an application that makes simulations.In the beginning of each simulation, FTP connection is created and after that some upload/download files operations are done. then simulation starts.After simulation finishes, again some upload file operations is required for back up simulation related folders

We encounter a problem if simulation takes more than 5 minutes.In the mean time, FTP connection is idle.I get following exceptions.

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:276)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:122)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:212)
at java.io.BufferedWriter.flush(BufferedWriter.java:236)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:477)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:537)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:586)
at org.apache.commons.net.ftp.FTP.quit(FTP.java:794)
at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:788)


Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:295)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:495)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:537)
at org.apache.commons.net.ftp.FTP.mkd(FTP.java:1363)
at org.apache.commons.net.ftp.FTPClient.makeDirectory(FTPClient.java:1971)

I tried following actions just after simulation is completed, I tried to reinitialize FTP , but no success.I see that connection is active in FTP side

1)FTPClient.isConnected() method returns true

2)When I call FTPClient.connect(), it throws exception FTP is already initialized

3)I tried to set timeouts in connect method of FTP.

public boolean connect() throws IllegalStateException, FileTransferException {
    /*
     * Precondition checks
     */
    final FTPClient clientBefore = this.getClient();
    if (clientBefore != null && clientBefore.isConnected()) {
        throw new IllegalStateException("FTP Client is already initialized");
    }
    // Create the client
    final FTPClient client = new FTPClient();
    client.setConnectTimeout(600000);
    client.setDefaultTimeout(600000);
    // FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
    // client.configure(conf);
    log.trace("Connecting to FTP Server at " + config.getCanonicalForm());
    try {
        client.connect(config.getHost(), config.getPort());
        client.setSoTimeout(600000);
    } catch (final IOException ioe) {
        throw new FileTransferException("Error in connecting to " + config.getCanonicalForm(),
                ioe);
    }
    log.trace("Connected to FTP Server at: " + config.getCanonicalForm());
    this.setClient(client);
    // Check that the last operation succeeded
    this.checkLastOperation();
    boolean result;
    try {
        // Login
        if (client.login(config.getUserName(), config.getPassword())) {
            result = true;
            // If there's a pwd defined, cd into it.
            final String pwd = this.getPresentWorkingDirectory();
            if (pwd != null) {
                this.cd(pwd);
            }
        } else {
            result = false;
        }
    } catch (final Exception e) {
        throw new FileTransferException("Could not log in", e);
    }
    return result;
}

Now, I am on deadlock.I do not know what is the problem

4

3 回答 3

4

如果您确实必须保持连接处于活动状态,请对服务器运行NOOP 命令,以便控制连接保持活动状态。

要么手动执行:

client.sendNoOp();

或者设置客户端以固定的间隔速率执行此操作:

client.setControlKeepAliveTimeout(300); // set timeout to 5 minutes

但是,如果您可以避免浪费资源,在初始下载阶段后注销断开连接,请进行本地处理并再次连接/登录以进行上传。

于 2013-07-21T14:41:25.943 回答
1

请记住,除了其他人提到的 ftp 超时之外,通信路径中的任何防火墙/NAT 都可能超时,通常不到 5 分钟。

于 2013-07-21T17:31:14.660 回答
0

深入研究源代码。

当 FTPClient 建立控制连接时

client.connect(config.getHost(), config.getPort());

它使用一些代码像这样工作:

Socket socket = SocketFactory.getDefault().createSocket();

创建一个套接字以连接到 ftp 服务器。

如果可以,将 keepalive 选项设置为套接字,使用

socket.setKeepAlive(true);

等级制度:

├── SocketClient
│   ├── FTP
│   │   └── FTPClient
于 2017-12-14T16:16:46.797 回答