Ftp Server Side: import java.util.*; import java.net.*; import java.io.*; class FtpServer { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(12345); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); Scanner sc=new Scanner(System.in); System.out.print("Enter the target filename: "); String str=sc.nextLine(); FileOutputStream fout=new FileOutputStream(str); while(true) { int i=Integer.parseInt(din.readUTF()); if(i==-1) break; fout.write(i); } fout.close(); din.close(); s.close(); ss.close(); System.out.print("\n\nFile transfer successful..."); } } Client Side: import java.util.*; import java.net.*; import java.io.*; class FtpClient { public static void main(String args[]) throws Exception { Socket s=new Socket("localhost",12345); DataOutputStream dout =new DataOutputStream(s.getOutputStream()); Scanner sc=new Scanner(System.in); System.out.print("Enter the source filename: "); String str=sc.nextLine(); FileInputStream fin=new FileInputStream(str); while(true) { int i=fin.read(); dout.writeUTF(""+i); dout.flush(); if(i==-1) break; } s.close(); dout.close(); fin.close(); System.out.print("\n\nFile transfer successful..."); } }
Tags:
Network
0 comments