Thursday, 7 May 2015

FILE TRANSFER PROTOCOL PROGRAM IN JAVA

CLIENT

import java.net.*;
import java.io.*;

public class Fclient
{
public static void main(String[] args)throws IOException
{
Socket echosocket=null;
BufferedReader in=null;
try
{
echosocket=new Socket(InetAddress.getLocalHost(),95);
in=new BufferedReader(new InputStreamReader(echosocket.getInputStream()));
}
catch(UnknownHostException e)
{
System.out.println(“DON’T KNOW ABOUT HOST”);
System.exit(1);
}
catch(IOException e)
{
System.err.println(“couldn’t get i/o for the connection”);
System.exit(1);
}
String userinput;
while((userinput=in.readLine())!=null)
{
System.out.println(userinput);
}
in.close();
echosocket.close();
}

}

SERVER

import java.net.*;
import java.io.*;

public class Fserver
{
public static void main(String args[])throes IOException
{
ServerSocket serversocket null;
try
{
serversocket=new ServerSocket(95);
}

catch(IOException e)
{
System.out.println(“could not listen on port:95”);
System.exit(1);
}

Socket clientsocket=null;
try
{
clientsocket=serversocket.accept();
System.out.println(“connected to:”+clientsocket);
}

catch(IOException e)
{
System.err.println(“accept failed”);
System.exit(1);
}

try
{
PrintWriter out=new PrintWriter(clientsocket.getOutputStream(),true);
String inputline,outputline;
BufferedReader stdin=new BufferedReader(new   InputStreamReader(System.in));
System.out.println(“Enter data in server”);
String s=stdin.readLine();
File f=new File(s);
if(f.exists())
{
BufferedReader d=new BufferedReader(new FileReader(s));
String line;
while((line=d.readLine())!=null)
{
out.write(line);
out.flush();
}
d.close();
out.close();
}
}

catch(FileNotFoundException e)
{
}
clientsocket.close();
serversocket.close();
}
}




No comments:

Post a Comment