You are on page 1of 13

CN TERMINAL LAB GOPINATH K S 19IT030

1 ) Use connection-oriented and connectionless sockets to implement a basic File


Transfer Protocol (FTP).

AIM:

To implement a simple file transfer protocol (FTP) using connection-oriented and connectionless sockets.

PROCEDURE:

FileServer

➢ Create a package called Server.


➢ Import all the necessary packages including jaa.net and java.io.
➢ Create a public class and create instance for socket and assign the port.
➢ Create the socket.accept() function for the client to accept the request.
➢ Create the method to saveFile called savefile with client socket as the parameter.
➢ Create instances for Data Input stream and File Output Stream respectively.
➢ Implement counter mechanism to calculate the amount of bytes sent and decrement
counter
➢ to identify the remaining bytes and print it.
➢ Close the file stream and socket.

FileClient

➢ Create a package Client.


➢ Import all the necessary files and create a private Socket instance.
➢ Create a constructor FileClient with host port and filename as parameters.
➢ Connect to the socket using Socket() constructor
➢ Create sendfile method and create instances for DataInputStream and
FileOutputStream.
➢ Implement buffer to verify the receival of data and retransmit if not received.
➢ Print the message if received.
➢ Close the file stream and socket.
➢ End.

FlowChart :

Page 1
CN TERMINAL LAB GOPINATH K S 19IT030

CODE :

Server :

import java.io.DataInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class FileServer extends Thread

private ServerSocket ss;

public FileServer(int port)

try

ss = new ServerSocket(port);

Page 2
CN TERMINAL LAB GOPINATH K S 19IT030

catch (IOException e)

e.printStackTrace();

public void run()

while (true)

try

Socket clientSock = ss.accept();

saveFile(clientSock);

catch (IOException e)

e.printStackTrace();

private void saveFile(Socket clientSock) throws IOException

DataInputStream dis = new DataInputStream(clientSock.getInputStream());

FileOutputStream fos = new FileOutputStream("D:/19IT030_LAB/out.txt");

Page 3
CN TERMINAL LAB GOPINATH K S 19IT030

byte[] buffer = new byte[4096];

int filesize = 15123;

int read = 0;

int totalRead = 0;

int remaining = filesize;

while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0)

totalRead += read;

remaining -= read;

System.out.println("read " + totalRead + " bytes.");

fos.write(buffer, 0, read);

fos.close();

dis.close();

public static void main(String[] args)

FileServer fs = new FileServer(1988);

fs.start();

Client :

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.IOException;

Page 4
CN TERMINAL LAB GOPINATH K S 19IT030

import java.net.Socket;

public class FileClient

private Socket s;

public FileClient(String host, int port, String file)

try

s = new Socket(host, port);

sendFile(file);

catch (Exception e)

e.printStackTrace();

public void sendFile(String file) throws IOException

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

FileInputStream fis = new FileInputStream(file);

byte[] buffer = new byte[4096];

while (fis.read(buffer) > 0)

dos.write(buffer);

Page 5
CN TERMINAL LAB GOPINATH K S 19IT030

System.out.println("File sent...");

fis.close();

dos.close();

public static void main(String[] args)

FileClient fc = new FileClient("localhost", 1988, "D:/19IT030_LAB/in.txt");

OUTPUT SCREENSHOTS:

Page 6
CN TERMINAL LAB GOPINATH K S 19IT030

RESULT:

Thus an application for the File transfer from the server to the client has been implemented and verified.

Page 7
CN TERMINAL LAB GOPINATH K S 19IT030

2 ) Using RMI, create a remote service request-response application for a hospital's


outpatient enrollment system

AIM:

To implement a remote service request-response application using Remote Method Invocation (RMI)
in Java for a hospital’s outpatient enrollment system.

PROCEDURE:

MyClient.java

➢ Import the package for remote method invocation and the util. package.
➢ Provide the lookup option in Naming to the method.
➢ Declare the variables.
➢ Get Voter ID from the user.
➢ Pass the voter ID entered by the user to the checkvote method to verify the voter ID using stub.
➢ Handle the returned values appropriately to the applicable conditions.
➢ When verified get the choice of selection to vote from the user
➢ Pass it to another method using instance of Vote in the method called vote().
➢ Appropriately handle the returned values.
➢ Get authorized password for verification and pass it to checkresult() method.
➢ Appropriately handle the returned values.
➢ Handle exceptions caused using try catch block.
➢ End.

MyServer.java

➢ Import the package used for remote method invocation.


➢ Import the package for RMI registry.
➢ Create an object to be passed to the remote method.
➢ Rebind using the rebind property by providing the hostname.
➢ Handle exceptions for this.
➢ End

Page 8
CN TERMINAL LAB GOPINATH K S 19IT030

Flowchart:

CODE :

Adder –

import java.rmi.*;

public interface Adder extends Remote

public int add(int x, int y,int z) throws RemoteException;

AdderRemote:

import java.rmi.*;

import java.rmi.server.*;

public class AdderRemote extends UnicastRemoteObject implements Adder

{
Page 9
CN TERMINAL LAB GOPINATH K S 19IT030

AdderRemote()throws RemoteException

super();

public int add(int x,int y,int z)

return x+y+z;

MyClient:

import java.rmi.*;

public class MyClient

public static void main(String args[])

try

Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");

System.out.println("NAME : GOPINATH K S");

System.out.println("AGE : 22");

System.out.println("TYPE : OP");

System.out.println("DOCTOR FEES : 2000");

System.out.println("ROOM FEES : 1500");

System.out.println("ADMISSION FEE : 100");

System.out.println("TOTAL : "+stub.add(2000,1500,100));

Page 10
CN TERMINAL LAB GOPINATH K S 19IT030

catch(Exception e){}

MyServer:

import java.rmi.*;

import java.rmi.registry.*;

public class MyServer

public static void main(String args[])

try

Adder stub=new AdderRemote(); Naming.rebind("rmi://localhost:5000/sonoo",stub);

catch(Exception e)

System.out.println(e);

Page 11
CN TERMINAL LAB GOPINATH K S 19IT030

OUTPUT SCREENSHOTS:

Server :

Client:

Page 12
CN TERMINAL LAB GOPINATH K S 19IT030

RESULT :

Thus, we have successfully implemented a remote service request-response application using Remote
Method Invocation (RMI) in Java for for a hospital’s outpatient enrollment system.

Page 13

You might also like