0% found this document useful (0 votes)
36 views

19P220 Lab - 8

The document describes an algorithm to implement a file transfer protocol (FTP) using Java. The algorithm has two parts - a server and a client. The server accepts connections from clients, receives file data, and writes it to a file. The client connects to the server, reads a file, and sends the file contents to the server. The program includes Java code for an FTP client and server class that implement this algorithm for file transfer.

Uploaded by

Maheshwaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

19P220 Lab - 8

The document describes an algorithm to implement a file transfer protocol (FTP) using Java. The algorithm has two parts - a server and a client. The server accepts connections from clients, receives file data, and writes it to a file. The client connects to the server, reads a file, and sends the file contents to the server. The program includes Java code for an FTP client and server class that implement this algorithm for file transfer.

Uploaded by

Maheshwaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EX.

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...");

Socket socket=new Socket("localhost",5000);


DataInputStream readInput = new DataInputStream(socket.getInputStream());
DataOutputStream writeOutput = new
DataOutputStream(socket.getOutputStream());
Scanner ip=new Scanner(System.in);

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();
}

//write to stream output


writeOutput.writeUTF(transfer);

System.out.println("Transfer done!\nClosing connection");

// 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;

public class FTPServer {


public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ServerSocket server=new ServerSocket(5000);

System.out.println("Server started");
System.out.println("Waiting for a client ...");

Socket socket = server.accept();

System.out.println("Client has joined sharing ");

DataInputStream readInput = new DataInputStream(socket.getInputStream());


DataOutputStream writeOutput = new
DataOutputStream(socket.getOutputStream());

Scanner ip = new Scanner(System.in);

// get Stream socket input


String lines=readInput.readUTF();

//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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy