Sie sind auf Seite 1von 5

Develop a client server application which implements File Transfer protocol.

Let the
client side request for files and the server reads it and sends to the client.
Input and Output Requirements:
The client accepts filename through the keyboard and sends it to the server. The server
reads the file and transfers the content back to the client.
Algorithm: server side
Begin
Create an object of server socket class and listen for incoming client connection in a
specified port.
When a client connection is established, create input and output streams for this connection.
While (true)
a. Read a file name from the input stream.
b. Read the content of the file requested by the client.
c. Send the file content to the client.
d. Close server and client socket.
End

Algorithm: client side


Begin
Establish a connection to the server program. Use IP address and port number of the server.
Crete input and output streams for the established connection.
Read filename from the keyboard and send to the server.
Display contents of file sent to the server.
Close the connection.
End
Program:
//FTP Server…..

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

public class FTP_Server


{
ServerSocket server; // create a server.....
Socket con= null; // to establish a connection
ObjectOutputStream out;
ObjectInputStream in;
String filename = "";
String contents = "";
File file=null;
void start() throws UnknownHostException,IOException ,FileNotFoundException
{
try
{
server = new ServerSocket(2004,10); // port,max Queue.....
System.out.println("Server - listening on port " + 2004);
System.out.println("Server - Waiting for the Client.....");
con = server.accept();
System.out.println("Server - Connection received from " +
con.getInetAddress().getHostName());
out = new ObjectOutputStream(con.getOutputStream());
in = new ObjectInputStream(con.getInputStream());
serverMessage("Server - Connection to Client Failed.....");

do
{
try
{
filename = (String) in.readObject();
System.out.println("Server > Got filename From Client -> " + filename);
File file = new File(filename);
FileInputStream fin = new FileInputStream(file);
DataInputStream din = new DataInputStream(fin);
while((contents = din.readLine()) != null)
{
System.out.println("Inside" + contents);
serverMessage(contents); //sending file contents.....
}
if(filename.equals("bye"))
serverMessage("bye");
}
catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}
}while(!filename.equals("bye"));
}
catch(IOException e){}
finally
{
try
{
in.close();
out.close();
server.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
void serverMessage(String str) throws IOException
{
try
{
out.writeObject(str); // WRITING TO SERVER'S OUTPUT
BUFFER.....
out.flush(); // clearing the output buffer.....
System.out.println("Server -writing Message to Server's output buffer....." + str);
}
catch(IOException ioe){ }
}

public static void main(String[] args) throws UnknownHostException


,IOException ,NullPointerException
{
FTP_Server server = new FTP_Server();
while(true)
{
try
{
server.start();
}
catch(NullPointerException e){}
}
}
}

//FTP Client …..

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

public class FTP_Client


{
Socket client = null; // create a client.....
String file = "test.txt";
ObjectOutputStream out;
ObjectInputStream in;
String msg;

void start() throws UnknownHostException,IOException


{
System.out.println("Filename : "+ file);
try
{
client = new Socket("localhost",2004); //inetaddress ip ,port
System.out.println("Client > Connecting to server.....");
out = new ObjectOutputStream(client.getOutputStream());
in=new ObjectInputStream(client.getInputStream());
ClientMessage("Client > Connection to server Failed.....");

do
{
try
{
msg = (String)in.readObject(); System.out.println("Client
> Message From Server ->" + msg);
ClientMessage(file);
}
catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}
}while(!msg.equals("bye"));
}
catch(IOException e){}
}
void ClientMessage(String str) throws IOException
{
try
{
System.out.println("Client > Writing to Client's output
buffer....." + str );
out.writeObject(str); // WRITING TO CLIENT'S OUTPUT BUFFER.....
out.flush(); // clearing the output buffer.....

}
catch(IOException ioe){}
}
public static void main(String[] args) throws UnknownHostException ,IOException
{
FTP_Client client = new FTP_Client();
client.start();
}
}
Output:
Server side
Server – listening on port 2004
Server – Waiting for the Client…..
Server > Got filename from Client -> test.txt
Client Side
Filename : test.txt
Client > Connecting to server…..
Client > message from server -> Hello I am sandeep.

Das könnte Ihnen auch gefallen