Java Program to Implementation of File Transfer Protocol

Aim:

  • To create file transfer protocol for transferring file form one location to another.

Requirement:

  • PC with UNIX/ Linux Operating systems.
  • Java

Algorithm:

Client:

  • Start the program
  • Include necessary packages such as java.net., java.io., java.lan.Object, java.util.*
  • Create a client socket
  • Get the local host address.
  • Create the file in write mode
  • Read content from the network pipe.
  • Write the content to the opened file.
  • End the program

Server:

  • Start the program
  • Include necessary packages such as java.net., java.io., java.lan.Object, java.util.*
  • Create a Server socket
  • Get the local host address.
  • Create the file in Read mode
  • Write the content to the network pipe.
  • Sent to the Client.
  • End the program.

Program:

ftpserver.java

import java.io.*; import java.net.*; public class ftpserver { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(8000); Socket s=ss.accept(); FileInputStream fin= new FileInputStream("vat.txt"); OutputStreamWriter out=new OutputStreamWriter(s.getOutputStream()); int i; while(true) { i=fin.read(); out.write(i); out.flush(); if(i==-1) break; } s.close(); fin.close(); System.out.println("File sent successful"); } }
Code language: JavaScript (javascript)

ftpclient.java

import java.io.*; import java.net.*; public class ftpclient { public static void main(String args[]) throws Exception { Socket s=new Socket(InetAddress.getLocalHost(),8000); FileOutputStream fout= new FileOutputStream("cat.txt"); InputStreamReader sin=new InputStreamReader(s.getInputStream()); int i; while(true) { i=sin.read(); if(i==-1|| i==63) break; fout.write(i); }s.close(); fout.close(); System.out.println("File received successful"); } }
Code language: JavaScript (javascript)

Run the Program:

  • Open 2 Command Prompt in that program folder.
  • 1st for ftpserver:
javac ftpserver.java: java ftpserver
Code language: CSS (css)
  • 2nd for ftpclient:
javac ftpclient.java java ftpclient
Code language: CSS (css)

Result:

Implementation of File Transfer Protocol / The transferring file from one location to another was sent successfully and verified.

Leave a Comment