Let's deep dive into RMI and Socket.

What will you learn?

  1. RMI Introduction
  2. RMI architecture
  3. RMI program: How to program in Java?
  4. Socket Introduction
  5. About TCP and UDP socket
  6. Socket program to communicate between client and server
  7. TCP socket vs. UDP socket in table

RMI Introduction

Remote Method Invocation (RMI) is a technique for establishing communication between Java applications. It provides a mechanism to provide distributed applications in Java. It includes the main package java.rmi.

RMI architecture:

In an RMI application, we write two programs, a server program (resides on the server) and a client program (resides on the client). It includes stub, skeleton, Remote Reference Layer, and Transport Layer.

RMI Program: How to program in Java??

  1. Defining a remote interface
  2. Implementing the remote interface
  3. Implement the server
  4. Implement the client
  5. Compile the source file
  6. Start the Java RMI registry
  7. Start the server
  8. Run the client

Client.java

//client program
import java.rmi.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Client {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = sc.nextInt();
        System.out.print("Enter second number: ");
        int b = sc.nextInt();
        try {
            Rem remobj = (Rem) Naming.lookup("rmi://localhost/Rem");
            System.out.println("Sum = " + remobj.addNum(a, b));
            System.out.println("Difference = " + remobj.subNum(a, b));
        } catch (RemoteException re) {
            System.out.println(re);
        } catch (NotBoundException nbe) {
            System.out.println(nbe);
        } catch (MalformedURLException mfe) {
            System.out.println(mfe);
        }
    }
}

Rem.java

//define remote interface
import java.rmi.*;

public interface Rem extends Remote{
  public int addNum(int a, int b) throws RemoteException;
  public int subNum(int a, int b) throws RemoteException;
}

Rem_impt.java

//implementation of interface
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class Rem_impt extends UnicastRemoteObject implements Rem{
  public Rem_impt() throws RemoteException{}

  public int addNum(int a, int b){
    return(a+b);
  }

  public int subNum(int a, int b){
    return(a-b);
  }
}

Server.java

//implementation of interface
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class Rem_impt extends UnicastRemoteObject implements Rem{
  public Rem_impt() throws RemoteException{}

  public int addNum(int a, int b){
    return(a+b);
  }

  public int subNum(int a, int b){
    return(a-b);
  }
}

 

Socket programming in Java - Chatting between client and server | Difference between TCP socket and UDP socket

Socket Introduction

A socket in Java is one endpoint of a two-way collaboration between two programs (for example, client and server). A socket is connected to a port number so that the TCP layer may detect that one application wants to connect with another program that has the same port number configured.

Questions:

  • What is a UDP socket? Differentiate it with a TCP socket.
  • What is a socket? How can you communicate two programs in a network using TCP Socket?
  • What is a TCP socket? Differentiate it with a UDP socket.
  • What is a socket? Differentiate TCP socket from UDP socket.
  • What is a socket? How can Java programs communicate with one another over TCP sockets? Discuss with a suitable example.
  • Create a Java program that uses TCP to allow client and server communication.

About TCP and UDP socket

TCP socket:

TCP provides a secure, point-to-point communication channel for client-server applications to connect with one another on the Web. A client program and a server program establish a connection to interact over TCP. Each program associates a socket with the end of the connection. The client and server communicate by reading and writing to the socket linked to the connection.

UDP socket:

UDP socket uses user datagram protocol (UDP). The User Datagram Protocol (UDP) runs on top of the Internet Protocol (IP) and was developed for applications that do not require reliability, acknowledgment, or flow control features at the transport layer. This simple protocol provides transport layer addressing in the form of UDP ports and an optional checksum capability.

Socket program to communicate between client and server:

client.java

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

public class client{
	public static void main(String[] args) throws IOException{
		
		//1. make a socket connection to the server.
		Socket conn = new Socket("localhost",95);

		//2. Get the socket I/O stream and perform the processing
		//2.1 --> InputStream; to receive information from server
		//2.2 --> OutputStream; to send information to the server
		PrintWriter out = new PrintWriter(conn.getOutputStream());
		out.println("hello server");
		out.flush();

		BufferedReader in = new BufferedReader(
			new InputStreamReader(conn.getInputStream())
			);

		String str = in.readLine();
		System.out.println("server: "+str);

		//3. close the connection
		in.close();
		out.close();
		conn.close();

	}
}

server.java

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

public class server{
	public static void main(String[] args) throws IOException{
		//1. create a server socket
		ServerSocket server = new ServerSocket(95);

		//2. wait for a connection
		System.out.println("Waiting for the client.....");
		Socket conn = server.accept();
		System.out.println("client connected!!");


		//3. Get the socket I/O stream and perform the processing
		//3.1 --> InputStream; to receive information from client
		//3.2 --> OutputStream; to send information to the client
		BufferedReader in = new BufferedReader(
			new InputStreamReader(conn.getInputStream())
			);

		String str = in.readLine();
		System.out.println("client: "+str);

		PrintWriter out = new PrintWriter(conn.getOutputStream());
		out.println("hi client");
		out.flush();

		//4. Close the connection
		in.close();
		out.close();
		conn.close();

	}
}

 

Differences between TCP and UDP:

S.N.             TCP                 UDP
i.                      TCP is a connection-oriented protocol.  UDP is a connection-less protocol. 
ii.                    TCP is reliable as it guarantees the delivery of data to the destination router. The delivery of data to the destination cannot be guaranteed in UDP.
iii.                 TCP is comparatively slower than UDP. UDP is faster, simpler, and more efficient than TCP.
iv.                  Retransmission of lost packets is possible in TCP. There is no retransmission of lost packets in User Datagram Protocol (UDP).
v.                    TCP is heavy-weight. UDP is lightweight.
vi.                  TCP header size is 20 bytes. UDP Header size is 8 bytes.
vii.               TCP is used by HTTP, FTP, SMTP Telnet etc. UDP is used by DNS, RIP (Routing information protocol), VoIP (voice over internet protocol), etc.

 

Conclusion:

In this way, we can write an RMI program and socket program. Using RMI and Socket, we can communicate between client and server. I hope the given code (RMI program and Socket program) is very clear to you.

I hope this post is very helpful to you. If you have any questions, don't hesitate to ask me in the comment section. I will reply as soon as possible. Thanks.