0% found this document useful (0 votes)
2 views5 pages

Socket RMI IUH Code Solutions

The document provides full code examples for various socket and RMI exercises in Java. It includes a TCP server that converts strings to uppercase, a multi-threaded server that calculates factorials, a UDP server that checks for prime numbers, and an RMI calculator with addition, subtraction, multiplication, and division functionalities. Each section contains both server and client implementations for the respective exercises.

Uploaded by

Hoàng Tuấn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Socket RMI IUH Code Solutions

The document provides full code examples for various socket and RMI exercises in Java. It includes a TCP server that converts strings to uppercase, a multi-threaded server that calculates factorials, a UDP server that checks for prime numbers, and an RMI calculator with addition, subtraction, multiplication, and division functionalities. Each section contains both server and client implementations for the respective exercises.

Uploaded by

Hoàng Tuấn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Giải Các Bài Tập Socket & RMI – Full Code

Bài 1 – TCP Server chuyển chuỗi thành IN HOA


SERVER:

import [Link].*;
import [Link].*;

public class UppercaseServer {


public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(5000);
[Link]("Server running...");

while (true) {
Socket socket = [Link]();
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter pw = new PrintWriter([Link](),
true);

String msg = [Link]();


[Link]([Link]());

[Link]();
}
}
}

CLIENT:

import [Link].*;
import [Link].*;

public class UppercaseClient {


public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 5000);

BufferedReader br = new BufferedReader(new


InputStreamReader([Link]()));
PrintWriter pw = new PrintWriter([Link](),
true);

[Link]("hello world");
[Link]("Server reply: " + [Link]());

[Link]();
}
}

Bài 2 – Server đa luồng tính giai thừa


SERVER:

import [Link].*;
import [Link].*;

class ClientHandler extends Thread {


Socket socket;
public ClientHandler(Socket s) { [Link] = s; }

public long factorial(int n) {


long f=1;
for(int i=1;i<=n;i++) f *= i;
return f;
}

public void run() {


try {
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter pw = new PrintWriter([Link](),
true);

int n = [Link]([Link]());
[Link]("n! = " + factorial(n));

[Link]();
} catch (Exception e) {}
}
}

public class FactorialServer {


public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(6000);
[Link]("Server running...");

while (true) new ClientHandler([Link]()).start();


}
}

CLIENT:

import [Link].*;
import [Link].*;

public class FactorialClient {


public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 6000);

BufferedReader br = new BufferedReader(new


InputStreamReader([Link]()));
PrintWriter pw = new PrintWriter([Link](), true);

[Link](5);
[Link]([Link]());

[Link]();
}
}

Bài 3 – UDP kiểm tra số nguyên tố


SERVER:

import [Link].*;

public class UDPPrimeServer {


public static boolean isPrime(int n) {
if (n < 2) return false;
for (int i=2;i<=[Link](n);i++)
if (n % i == 0) return false;
return true;
}

public static void main(String[] args) throws Exception {


DatagramSocket ds = new DatagramSocket(7000);
byte[] buffer = new byte[1024];

while (true) {
DatagramPacket dp = new DatagramPacket(buffer,
[Link]);
[Link](dp);

int num = [Link](new String([Link](), 0,


[Link]()));
String result = isPrime(num) ? "Prime" : "Not Prime";

byte[] res = [Link]();


DatagramPacket reply = new DatagramPacket(
res, [Link], [Link](), [Link]()
);
[Link](reply);
}
}
}
CLIENT:

import [Link].*;

public class UDPPrimeClient {


public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
byte[] data = "17".getBytes();

DatagramPacket dp = new DatagramPacket(data, [Link],


[Link]("localhost"), 7000);
[Link](dp);

byte[] buffer = new byte[1024];


DatagramPacket reply = new DatagramPacket(buffer,
[Link]);
[Link](reply);

[Link](new String([Link](), 0,
[Link]()));
[Link]();
}
}

PHẦN RMI – Code giải bài tập

Bài 4 – RMI Calculator


INTERFACE:

import [Link].*;

public interface Calculator extends Remote {


int add(int a, int b) throws RemoteException;
int sub(int a, int b) throws RemoteException;
int mul(int a, int b) throws RemoteException;
int div(int a, int b) throws RemoteException;
}

IMPLEMENTATION:

import [Link].*;
import [Link].*;

public class CalculatorImpl extends UnicastRemoteObject implements


Calculator {
protected CalculatorImpl() throws RemoteException {}
public int add(int a, int b) { return a+b; }
public int sub(int a, int b) { return a-b; }
public int mul(int a, int b) { return a*b; }
public int div(int a, int b) { return a/b; }
}

SERVER:

import [Link].*;

public class CalcServer {


public static void main(String[] args) throws Exception {
Calculator cal = new CalculatorImpl();
[Link](1099);
[Link]("rmi://localhost/calc", cal);
[Link]("Calculator Server Ready");
}
}

CLIENT:

import [Link].*;

public class CalcClient {


public static void main(String[] args) throws Exception {
Calculator cal = (Calculator)
[Link]("rmi://localhost/calc");
[Link]("3 + 4 = " + [Link](3,4));
}
}

You might also like