编程是在 localhost 上模拟 FTP 服务器和客户端。服务器应该在 22222 端口监听客户端的命令,并在 22223 端口创建另一个连接来传输数据。我已经在同一个端口 22222 上实现了传输。问题是如何实际分离一个连接来接收命令:“get”,“put”并在不同的端口上创建另一个连接来传输数据?有人能帮帮我吗?
FTPServer.class
public class FTPServer {
public static void main(String args[]) throws Exception
{
int PORT =22222;
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("FTP Server Listening on Port " + PORT);
ExecutorService executor = Executors.newFixedThreadPool(10);
while(true)
{
System.out.println("Server is waiting...");
FileTransfer ft = new FileTransfer(serverSocket.accept());
executor.execute(ft);
}
}
}
class FileTransfer extends Thread
{
Socket clientSocket;
DataInputStream inFromClient;
DataOutputStream outToClient;
String filePath = "E:" +File.separator+"Studying"+
File.separator+"WorkSpace"+File.separator+"FTPServer"+File.separator;
FileTransfer(Socket socket)
{
try
{
clientSocket=socket;
inFromClient = new DataInputStream(clientSocket.getInputStream());
outToClient = new DataOutputStream(clientSocket.getOutputStream());
}catch(Exception ex){}
}
public void ClientDownLoad() throws Exception
{
String fileName = inFromClient.readUTF();
String accessfile = filePath + fileName;
File file = new File(accessfile);
if(!file.exists())
{
outToClient.writeUTF("FILE NOT FOUND");
return;
}
else
{
outToClient.writeUTF("Ready for download");
FileInputStream f = new FileInputStream(file);
int numOfByte;
do
{
numOfByte = f.read();
outToClient.writeUTF(String.valueOf(numOfByte));
}while(numOfByte!=-1);
f.close();
outToClient.writeUTF("Download Completed");
}
}
public void ClientUpLoad() throws Exception
{
String fileName = inFromClient.readUTF();
String userChoose;
String accessFile = filePath + fileName;
File file=new File(accessFile);
if(fileName.equalsIgnoreCase("No such file"))
{
return;
}
if(file.exists())
{
outToClient.writeUTF("File Exists in Server");
userChoose=inFromClient.readUTF();
}
else
{
outToClient.writeUTF("OK...You can send file");
userChoose="y";
}
if(userChoose.equalsIgnoreCase("y"))
{
FileOutputStream fileOut = new FileOutputStream(file);
int numOfByte;
String str;
do
{
str=inFromClient.readUTF();
numOfByte=Integer.parseInt(str);
if(numOfByte!=-1)
{
fileOut.write(numOfByte);
}
}while(numOfByte!=-1);
fileOut.close();
outToClient.writeUTF("Uploading Completed");
}
else
{
return;
}
}
public void run()
{
while(true)
{
try
{
String str = inFromClient.readUTF();
if(str.equalsIgnoreCase("put"))
{
System.out.println("Accept Uploading...");
ClientUpLoad();
continue;
}
else if(str.equalsIgnoreCase("get"))
{
System.out.println("Accept Downloading");
ClientDownLoad();
continue;
}
else if(str.equalsIgnoreCase("exit"))
{
System.out.println("Accept Exit");
System.exit(1);
}
}catch(Exception e){}
}
}
}
FTPClient.class
public class FTPClient {
public static void main(String[] args) throws Exception
{
final int PORT=22222;
String serverHostname = new String("127.0.0.1");
Socket clientSocket = new Socket(serverHostname, PORT);
clientOperation co=new clientOperation(clientSocket);
co.displaySelection();
}
}
class clientOperation
{
String filePath = "E:" +File.separator+"Studying"+
File.separator+"WorkSpace"+File.separator+"FTPClient"+File.separator;
Socket socket;
DataInputStream inFromServer;
DataOutputStream outToServer;
BufferedReader br;
clientOperation(Socket s)
{
try
{
socket=s;
inFromServer=new DataInputStream(socket.getInputStream());
outToServer=new DataOutputStream(socket.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
}catch(Exception e){}
}
public void ClientUpLoad() throws Exception
{
String fileName;
System.out.println("Enter File Name for uploading: ");
fileName=br.readLine();
String accessfile=filePath + fileName;
System.out.println(accessfile);
File file = new File(accessfile);
if(!file.exists())
{
System.out.println("File Not Found on Client");
outToServer.writeUTF("no such file");
return;
}
outToServer.writeUTF(fileName);
String existingMsg = inFromServer.readUTF();
if(existingMsg.compareToIgnoreCase("File Exists in Server")==0)
{
System.out.println("File Exists in Server. Do you want to replace it? (Y/N)");
String userChoose=br.readLine();
if(userChoose.equalsIgnoreCase("y"))
{
outToServer.writeUTF("y");
}
else if(userChoose.equalsIgnoreCase("n"))
{
outToServer.writeUTF("n");
return;
}
else{
}
}
System.out.println("Uploading...");
FileInputStream fi = new FileInputStream(file);
int numOfByte;
do
{
numOfByte=fi.read();
outToServer.writeUTF(String.valueOf(numOfByte));
}while(numOfByte!=-1);
fi.close();
System.out.println(inFromServer.readUTF());
}
public void ClientDownLoad()throws Exception
{
String fileName;
String userChoose;
System.out.println("Enter File Name for download: ");
fileName=br.readLine();
outToServer.writeUTF(fileName);
String str = inFromServer.readUTF();
if(str.equalsIgnoreCase("FILE NOT FOUND"))
{
System.out.println("File doesn't exist on server...");
return;
}
else if(str.equalsIgnoreCase("Ready for download"))
{
String accessfile=filePath + fileName;
File file=new File(accessfile);
if(file.exists())
{
System.out.println("File exists in Client. Do you want to replace it? (Y/N)");
userChoose=br.readLine();
if(userChoose.equalsIgnoreCase("n"))
{
outToServer.flush();
return;
}
}
FileOutputStream fileOut = new FileOutputStream(file);
int numOfByte;
do
{
numOfByte=Integer.parseInt(inFromServer.readUTF());
if(numOfByte!=-1)
{
fileOut.write(numOfByte);
}
}while(numOfByte!=-1);
fileOut.close();
System.out.println(inFromServer.readUTF());
}
}
public void displaySelection() throws Exception
{
while(true)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please select the number for purpose!");
System.out.println("1. Upload File to Server");
System.out.println("2. Download File from Server");
System.out.println("3. Exit");
System.out.println("Enter your choice: ");
int choice = scan.nextInt();
if(choice ==1)
{
outToServer.writeUTF("put");
ClientUpLoad();
}
else if(choice == 2)
{
outToServer.writeUTF("get");
ClientDownLoad();
}
else
{
outToServer.writeUTF("exit");
System.exit(1);
}
}
}
}