Mastering Java RMI and Socket Programming
🕒 2025-04-23 02:55:45.673149Let's deep dive into RMI and Socket.
What will you learn?
- RMI Introduction
- RMI architecture
- RMI program: How to program in Java?
- Socket Introduction
- About TCP and UDP socket
- Socket program to communicate between client and server
- TCP socket vs. UDP socket in table
RMI Introduction
RMI architecture:
RMI Program: How to program in 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);
}
}
}
//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;
}
//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);
}
}
//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
About TCP and UDP socket
Socket program to communicate between client and server:
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();
}
}
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:
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.
Comments
Loading comments...
Leave a Comment