19P220 Lab - 8
19P220 Lab - 8
NO:8
IMPLEMENTATION OF FILE TRANSFER PROTOCOL
DATE:
Aim:
To write a program to implement File Transfer Protocol.
Algorithm:
Server :
Step 1: Start the program
Step 2 : Import the java packages and create class file server.
Step 3: String of argument is passed to all arg[].
Step 4: Create a new server socket and bind it to the port.
Step 5: Accept the client connection at the requested port.
Step 6: Get the file name and stored into Buffered Reader.
Step 7: Create a new object class file and readline.
Step 8: If file is exist then file reader read the content until EOF is reached.
Step 9: Else print Filename doesn’t exists. Step
10: End of main.
Step 11: End of File main class.
Client:
Step 1: Import the java packages and create class file client.
Step 2: String of argument is passed to all arg[].
Step 3: Connection between the client and the server has been successfully established. Step
4: The object of a Buffer Reader class is used for storing data content which have been
retrieved from socket object.
Step 5: The content are read and stored in inp until the EOF is reached.
Step 6: The content are displayed in client window and the connection was closed.
Step 7: End of main.
Step 8: End of File main class.
Step 9:Stop the program.
PROGRAM:
FTPClient
package dccn;
19P220
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.net.Socket;
import java.util.Scanner;
public class FTPClient {
public static void main(String arg[]) throws Exception {
System.out.println("Attempting to connect...");
System.out.println("Start transfering");
String transfer="";
// read file
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("E:\\file2.txt"));
String line = reader.readLine();
while (line != null) {
transfer=transfer+line+"\n";
line = reader.readLine();
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
// close connection
19P220
socket.close();
readInput.close();
writeOutput.close();
ip.close();
}
}
FTPServer
package dccn;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
System.out.println("Server started");
System.out.println("Waiting for a client ...");
//write to files
19P220
FileWriter myWriter = new FileWriter("E:\\file1.txt");
myWriter.write(lines);
myWriter.close();
server.close();
socket.close();
writeOutput.close();
readInput.close();
ip.close();
}
}
OUTPUT:
19P220
RESULT:
Thus the developed program is successfully executed and its output verified.
19P220