Java到FileZilla FTP客户端:套接字写错误

时间:2013-05-08 11:34:10

标签: java sockets ftp filezilla

这几天另一个挑战让我大吃一惊。我正在尝试用Java创建一个FTP服务器,它应该能够与FileZilla FTP客户端通信。代码不是最好的,因为我尝试了很多东西来找出发生了什么。

这里是从德语翻译的FileZilla日志

Status: Connect to 127.0.0.1:21...
Status: Connected!Waiting for welcome message.
Response:    220 localhost connected
Error:  Establishing connection to server failed.

到目前为止,主要问题是建立真正的连接。套接字是连接的,我可以向客户端发送至少一条消息,但没有机会获得客户端输入,例如PASV,USER和PASS。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.JButton;
import javax.swing.JFrame;

public class FTP_Server extends JFrame implements ActionListener, WindowListener {
    private static final long serialVersionUID = 1L;

    private Container cp;
    private JButton btncon;
    private ServerSocket listenSocket;
    private Socket connectionSocket;
    private DataOutputStream dos;
    private DataInputStream dis;
    private OutputStream os;
    private InputStream is;

    public FTP_Server() {
        super("FTP Server");

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        cp = this.getContentPane();
        cp.setLayout(new BorderLayout());

        btncon = new JButton("Connect");
        btncon.addActionListener(this);

        cp.add(btncon, BorderLayout.CENTER);

        this.pack();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(btncon)){
            createConnection();
        }
    }

    public void createConnection() {
        System.out.println("Connecting...");
        try {
            listenSocket = new ServerSocket(21);
            listenSocket.setSoTimeout(0);
            connectionSocket = listenSocket.accept();
            connectionSocket.setSoLinger(true, 0);
            connectionSocket.setSoTimeout(0);
            connectionSocket.setKeepAlive(true);
            System.out.println("Connected!");

            os = connectionSocket.getOutputStream();
            dos = new DataOutputStream(os);

            is = connectionSocket.getInputStream();
            dis = new DataInputStream(is);

            while(connectionSocket.isConnected() == true) {
                dos.writeUTF("220 localhost connected\r\n");
                //Will cause socket write error soon!
                dos.flush();
                dos.writeUTF("331 Anonym no password needed\r\n");
                System.out.println(is.read());
            }
        } catch (SocketException exp) {
            try {
                listenSocket.close();
                System.out.println("Disconnected");
                exp.printStackTrace();
            } catch (IOException exp2) {
                exp2.printStackTrace();
            }
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowIconified(WindowEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowActivated(WindowEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        // TODO Auto-generated method stub

    }
}

对于错误消息/打印堆栈:

java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.DataOutputStream.write(DataOutputStream.java:107)
    at java.io.DataOutputStream.writeUTF(DataOutputStream.java:401)
    at java.io.DataOutputStream.writeUTF(DataOutputStream.java:323)
    at ftp.FTP_Server.createConnection(FTP_Server.java:82)
    at ftp.FTP_Server.actionPerformed(FTP_Server.java:58)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

2 个答案:

答案 0 :(得分:0)

这里犯了很多错误。

  1. 不要乱用SO_LINGER。它没有必要,只会导致进一步的不必要的疼痛。
  2. writeUTF()写入只有readUTF()才能读取的格式。它不是与非Java客户端通信的方式。
  3. 您没有在单独的线程中处理已接受的套接字,因此您一次只能处理一个客户端。
  4. isConnected()不是连接状态的有效测试。它会告诉您是否连接了套接字。你做了,所以它会返回true。当对等设备断开连接时,这不会改变。您需要以常规方式测试EOS,并记录在各种读取API中。
  5. 在接受的套接字上获得IOException时,不需要关闭侦听套接字。
  6. 我不明白为什么FTP服务器需要GUI,但如果需要,它必须在与AWT线程不同的线程上执行所有网络I / O,包括接受连接。
  7. 您只需告诉客户他已成功连接一次,而不是无限次连接。
  8. 这足以继续下去。

答案 1 :(得分:0)

我仍然遇到消息传递问题。我现在所做的是为每个连接的套接字以及每个输出流创建一个新线程。

package ftp;

import java.io.DataOutputStream;
import java.io.IOException;

public class MultiIO implements Runnable{
    private DataOutputStream dos;

    public MultiIO(DataOutputStream stream) {
        dos = stream;
        System.out.println("Set OutputStream.");
    }

    @Override
    public void run() {
        try {
            dos.writeBytes("220");
            dos.flush();
            System.out.println("tried once");
        } catch (IOException e) {
            System.exit(-1);
        }
    }
}

现在的麻烦是FileZilla客户端不会收到220消息。

这是新的套接字处理类。我有点从oracle页面复制它。

package ftp;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;

public class MultiConnection implements Runnable{
    private Socket cSocket;

    public MultiConnection(Socket client) {
        cSocket = client;
        System.out.println("Connected!");
    }

    @Override
    public void run() {
        OutputStream os = null;
        DataOutputStream dos = null;
        try {
            os = cSocket.getOutputStream();
            dos = new DataOutputStream(os);
        } catch (SocketException es) {
            es.printStackTrace();
        } catch (IOException e) {
            System.exit(-1);
        }

        MultiIO mio;
        mio = new MultiIO(dos);
        Thread thio = new Thread(mio);
        thio.start();
    }
}