//for Server side
import java.net.*;
import java.io.*;
class FileManager
{
public static String getFileContent( String filename ) throws IOException
{
String content=”";
try
{
BufferedReader reader = new BufferedReader( new FileReader( filename ) );
String data;
while( ( data = reader.readLine() ) != null )
content = content + data + “\n”;
reader.close();
} catch( FileNotFoundException e)
{
System.out.println(”Request file does not exists”);
content = “-1″;
}
return content;
}
}
class Service implements Runnable
{
Thread t;
Socket client=null;
Service( Socket client )
{
this.client = client;
t = new Thread(this);
t.start();
}
public void run( )
{
try
{
DataInputStream din = new DataInputStream( client.getInputStream() );
DataOutputStream dout = new DataOutputStream( client.getOutputStream() );
String filename = din.readUTF();
System.out.println(”Requested file = ” + filename);
String data = FileManager.getFileContent( filename );
dout.writeUTF( data );
din.close();
dout.close();
}
catch( Exception e) {
System.out.println(e);
}
}
}
class ftpserver
{
public static void main( String v[] )
{
try
{
ServerSocket s = new ServerSocket( 2121 );
System.out.println(”Server started waiting for client request”);
System.out.println(”Ip address = ” + InetAddress.getLocalHost());
System.out.println(”Port address ( 2121 )”);
while( true )
{
new Service( s.accept() );
}
}
catch( Exception e) {
System.out.println(e);
}
}
}
//Code for Client Side
import java.net.*;
import java.io.*;
class ftpclient
{
public static void main( String v[] )
{
try{
Socket client = new Socket( “127.0.0.1″ , 2121 );
DataInputStream din = new DataInputStream( client.getInputStream() );
DataOutputStream dout = new DataOutputStream( client.getOutputStream() );
dout.writeUTF( v[0] ); // write filename to server
String content = din.readUTF(); // read content from the server
if( content.equals( “-1″))
{
System.out.println(”No such file exists at server”);
}
else
{
System.out.println( content );
}
}
catch( Exception e )
{
System.out.println(e);
}
}
}
//Executing Process
>javac ftpserver.java
>javac ftpclient.java
>java ftpserver
Open new Cmd prompt
——————–
>java ftpclient