这些天,另一个挑战让我头疼。我正在尝试用 Java 创建一个应该能够与 FileZilla FTP 客户端通信的 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)