/* IMPLEMENTATION OF FTP */ Server: import [Link].*; import [Link].*; import [Link].*; import [Link].
*; class server1 { ServerSocket ss; Socket s; BufferedReader netin; DataOutputStream netout; String req; public server1(int pno) throws IOException { ss=new ServerSocket(pno); [Link]("Server ready"); } public void getConnection() { s=null; try { [Link]("Waiting for client"); s=[Link](); [Link]("Connected to client:"+[Link]().getHostName()); netin=new BufferedReader(new InputStreamReader([Link]())); netout=new DataOutputStream([Link]()); [Link]("Ready for service"); } catch(Exception e) { [Link](e); } } public String getRequest() { [Link]("Waiting for request"); try { String input=null; while((input=[Link]())==null) [Link](1); return input; }
catch(Exception e2) { return null; } } public void service(String request) { try { StringTokenizer st= new StringTokenizer(request); String first=[Link](); if([Link]("Quit")) { [Link](); getConnection(); } else if([Link]("Get")) sendfile([Link]()); } catch(Exception e3) { } } public void sendfile(String fname) { try { File r=new File(fname); if(![Link]()) { [Link]("NFound"); [Link](); return; } [Link]("Found"); [Link](); FileInputStream fin=new FileInputStream(fname); int n=(int)[Link](); byte b[]=new byte[n]; [Link](b,0,n); [Link](b); [Link](); [Link]((byte)-1); [Link]();
[Link]("The file has been transmitted"); } catch(Exception e4) { } } } class server { public static void main(String a[]) { server1 m=null; try { m=new server1(3000); } catch(Exception e5) { } [Link](); do { String req=[Link](); [Link](req); } while(true); } } Client: import [Link].*; import [Link].*; import [Link].*; import [Link].*; class client1 { Socket s; DataInputStream netin; DataOutputStream netout; BufferedReader input; PrintStream pout; String req;
public client1(String server,int pno)throws IOException, UnknownHostException { s=new Socket(server,pno); [Link]("\n connected to server"); netin=new DataInputStream([Link]()); netout=new DataOutputStream([Link]()); input=new BufferedReader(new InputStreamReader([Link])); pout=new PrintStream([Link]()); } public void sendRequest() { try { [Link]("ENTER THE REQUEST"); req=[Link](); [Link](req); [Link](); getResponse(req); } catch(Exception e1) {} } public void getResponse(String req) { try { StringTokenizer st=new StringTokenizer(req); String first=[Link](); if([Link]("Quit")) { [Link](); [Link](0); } else if([Link]("Get")) getfile([Link]()); } catch(Exception e2) {} } public void getfile(String fname) {
try { String v; while((v=[Link]())==null) [Link](1); if([Link]("NFound")) { [Link]("\n file not found"); return; } [Link]("\n Enter the path to download the file"); v=[Link](); FileOutputStream fout=new FileOutputStream(v); int n; byte b; while((b=(byte)[Link]())!=-1) [Link](b); [Link](); [Link]("\n The file has been downloaded"); } catch(Exception e5) {} } } class client { public static void main(String arg[]) { client1 y=null; try { y=new client1("localhost",3000); } catch(Exception e6) {} do { [Link](); }while(true); } }
OUTPUT: Command prompt 1: z:\nlab> javac [Link] z:\nlab>java server Server ready Waiting for client Connected to client:localhost Ready for service Waiting for request The file has been transmitted Waiting for request Waiting for client Command prompt 2: z:\nlab> javac [Link] z:\nlab>java client connected to server ENTER THE REQUEST get c:\[Link] Enter the path to download the file e:\[Link] The file has been downloaded ENTER THE REQUEST quit