/* This program was written by S. Tanaka, E-mail: tanaka@nuee.nagoya-u.ac.jp */ import java.io.*; import java.net.*; public class FTPServer extends Thread { public static void main(String[] args) { if(args.length != 0) { // System.out.println(args[0]); r = args[0];} else{ r = "c:";} int i = 1; try { ServerSocket s = new ServerSocket(21); for(;;) { Socket incoming = s.accept(); new FTPServer(incoming,i).start(); i++; } } catch(Exception e){} } public FTPServer(Socket income, int c) { incoming = income; counter = c; } public void run() { int lng,lng1,lng2,i,ip1,ip2,ip = 1,h1; String a1,a2,di,str1,user="",host,dir; System.out.println(r); dir = r; InetAddress inet; InetAddress localip; try { inet = incoming.getInetAddress(); localip = inet.getLocalHost(); host = inet.toString(); h1 = host.indexOf("/"); host = host.substring(h1 + 1); // System.out.println(host); BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter(incoming.getOutputStream(),true); out.println("220 MyFtp server ready.\r"); boolean done = false; while(!done) { a1 = ""; a2 = ""; String str = in.readLine(); if(str.startsWith("RETR")){ out.println("150 Binary data connection"); str = str.substring(4); str = str.trim(); System.out.println(str); System.out.println(dir); RandomAccessFile outFile = new RandomAccessFile(dir+"/"+str,"r"); Socket t = new Socket(host,ip); OutputStream out2 = t.getOutputStream(); byte bb[] = new byte[1024]; int amount; try{ while((amount = outFile.read(bb)) != -1){ out2.write(bb, 0, amount); } out2.close(); out.println("226 transfer complete"); outFile.close(); t.close(); } catch(IOException e){} } if(str.startsWith("STOR")){ out.println("150 Binary data connection"); str = str.substring(4); str = str.trim(); System.out.println(str); System.out.println(dir); RandomAccessFile inFile = new RandomAccessFile(dir+"/"+str,"rw"); Socket t = new Socket(host,ip); InputStream in2 = t.getInputStream(); byte bb[] = new byte[1024]; int amount; try{ while((amount = in2.read(bb)) != -1){ inFile.write(bb, 0, amount); } in2.close(); out.println("226 transfer complete"); inFile.close(); t.close(); } catch(IOException e){} } if(str.startsWith("TYPE")){ out.println("200 type set");} if(str.startsWith("DELE")){ str = str.substring(4); str = str.trim(); File f = new File(dir,str); boolean del = f.delete(); out.println("250 delete command successful");} if(str.startsWith("CDUP")){ int n = dir.lastIndexOf("/"); dir = dir.substring(0,n); out.println("250 CWD command succesful"); } if(str.startsWith("CWD")){ str1 = str.substring(3); dir = dir+"/"+str1.trim(); out.println("250 CWD command succesful"); } if(str.startsWith("QUIT")) { out.println("GOOD BYE"); done = true; } if(str.startsWith("USER")){ user = str.substring(4); user = user.trim(); out.println("331 Password");} if(str.startsWith("PASS")) out.println("230 User "+user+" logged in."); if(str.startsWith("PWD")){ out.println("257 \""+dir+"\" is current directory"); } if(str.startsWith("SYS")) out.println("500 SYS not understood"); if(str.startsWith("PORT")) { out.println("200 PORT command successful"); lng = str.length() - 1; lng2 = str.lastIndexOf(","); lng1 = str.lastIndexOf(",",lng2-1); for(i=lng1+1;i