我正在设置一个基本的客户端服务器程序。现在,客户端只是将一个带有命令字符串的对象发送到服务器,服务器正在确认它。这是使用带有按钮的JFrame完成的。它似乎工作正常,除了客户端似乎发送多个请求而不是一个。
我的客户代码中的内部类:
private class CommandHandler implements ActionListener{
FTPCommand c;
ObjectOutputStream oos;
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd == "DIR"){
c = new FTPCommand("DIR");
}
if (cmd == "CHDIR"){
String newDirectory = JOptionPane.showInputDialog("Please enter directory to change to:");
c = new FTPCommand("CHDIR", newDirectory);
}
try {
oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject(c);
oos.flush();
//System.out.println("Command: " + c.getCommand() + ", sent successfully");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
服务器:
public void run(){
ObjectInputStream in;
try {
in = new ObjectInputStream(sock.getInputStream());
FTPCommand cmd = (FTPCommand) in.readObject();
System.out.println("Received command: " + cmd.getCommand() + " at " + System.currentTimeMillis());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
所以,如果我只点击DIR按钮一次,这就是我得到的输出,虽然有时可能只有一两行:
Received command: DIR at 1390572358017
Received command: DIR at 1390572365578
Received command: DIR at 1390572377229
为什么要发送多个请求?
答案 0 :(得分:0)
检查FTPCommand的构造函数;那多少次......它已被调用......如果三次......跟踪调用方法。
答案 1 :(得分:0)
事实证明,这是客户端保留以前的连接,并通过所有打开的套接字发送请求的结果。