OneCompiler

gui 2

61

// Server.java
import java.io.;
import java.net.
;

public class Server {

public static void main(String[] args) {

    try {
        // Create server socket on port 5000
        ServerSocket serverSocket = new ServerSocket(5000);
        System.out.println("Server started...");
        System.out.println("Waiting for client...");

        // Accept client connection
        Socket socket = serverSocket.accept();
        System.out.println("Client connected!");

        // Input and Output streams
        BufferedReader input = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

        PrintWriter output = new PrintWriter(
                socket.getOutputStream(), true);

        BufferedReader keyboard = new BufferedReader(
                new InputStreamReader(System.in));

        String message;

        while (true) {

            // Read message from client
            message = input.readLine();
            System.out.println("Client: " + message);

            // Stop if client says bye
            if (message.equalsIgnoreCase("bye")) {
                break;
            }

            // Send reply to client
            System.out.print("Server: ");
            String reply = keyboard.readLine();
            output.println(reply);

            if (reply.equalsIgnoreCase("bye")) {
                break;
            }
        }

        // Close connections
        socket.close();
        serverSocket.close();

    } catch (Exception e) {
        System.out.println(e);
    }
}

}


// Client.java
import java.io.;
import java.net.
;

public class Client {

public static void main(String[] args) {

    try {
        // Connect to server
        Socket socket = new Socket("localhost", 5000);

        System.out.println("Connected to server!");

        // Input and Output streams
        BufferedReader input = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

        PrintWriter output = new PrintWriter(
                socket.getOutputStream(), true);

        BufferedReader keyboard = new BufferedReader(
                new InputStreamReader(System.in));

        String message;

        while (true) {

            // Send message to server
            System.out.print("Client: ");
            message = keyboard.readLine();
            output.println(message);

            if (message.equalsIgnoreCase("bye")) {
                break;
            }

            // Receive reply from server
            String reply = input.readLine();
            System.out.println("Server: " + reply);

            if (reply.equalsIgnoreCase("bye")) {
                break;
            }
        }

        // Close socket
        socket.close();

    } catch (Exception e) {
        System.out.println(e);
    }
}

}


How to Run
Step 1: Compile both files
javac Server.java
javac Client.java
Step 2: Run Server first
java Server
Step 3: Run Client in another terminal
java Client
Output Example
Server Window
Server started...
Waiting for client...
Client connected!
Client: Hello
Server: Hi
Client Window
Connected to server!
Client: Hello
Server: Hi
Important Concepts
ServerSocket → waits for client connection
Socket → connects client and server
BufferedReader → reads messages
PrintWriter → sends messages
localhost → same computer
5000 → port number

This is the simplest client-server chat program using Java sockets.


Here’s a very simple Java RMI (Remote Method Invocation) example with:

Remote Interface
Remote Object (Implementation)
Server
Client

This program performs addition of two numbers remotely.

  1. Remote Interface
    AddInterface.java
    import java.rmi.Remote;
    import java.rmi.RemoteException;

// Remote interface
public interface AddInterface extends Remote {

// Remote method
int add(int a, int b) throws RemoteException;

}
2. Remote Object Implementation
AddImplementation.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

// Implementation class
public class AddImplementation extends UnicastRemoteObject
implements AddInterface {

// Constructor
protected AddImplementation() throws RemoteException {
    super();
}

// Method implementation
public int add(int a, int b) throws RemoteException {
    return a + b;
}

}
3. Server Program
Server.java
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class Server {

public static void main(String[] args) {

    try {
        // Create object
        AddImplementation obj = new AddImplementation();

        // Start RMI registry at port 1099
        LocateRegistry.createRegistry(1099);

        // Bind object with name
        Naming.rebind("rmi://localhost/AddService", obj);

        System.out.println("Server is running...");
    }

    catch (Exception e) {
        System.out.println(e);
    }
}

}
4. Client Program
Client.java
import java.rmi.Naming;

public class Client {

public static void main(String[] args) {

    try {
        // Lookup remote object
        AddInterface obj = (AddInterface)
                Naming.lookup("rmi://localhost/AddService");

        // Call remote method
        int result = obj.add(10, 20);

        System.out.println("Addition = " + result);
    }

    catch (Exception e) {
        System.out.println(e);
    }
}

}
Steps to Run
Step 1: Compile all files
javac *.java
Step 2: Run Server
java Server

Output:

Server is running...
Step 3: Run Client

Open another terminal:

java Client

Output:

Addition = 30
Simple RMI Flow
Client -----> Server
Remote Method Call
Client calls method
Server executes method
Result returned to client
Important Points for Viva
RMI = Remote Method Invocation
Used for communication between Java programs
Remote interface is compulsory
RemoteException must be handled
UnicastRemoteObject exports remote object
Naming.lookup() is used by client
Naming.rebind() is used by server