-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_server9.java
39 lines (30 loc) · 948 Bytes
/
09_server9.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.*;
import java.net.*;
public class server9 {
public static void main(String[] args) {
final int PORT = 12345;
try {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server started. Waiting for client...");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader reader = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
// Read two numbers from the client
int num1 = Integer.parseInt(reader.readLine());
int num2 = Integer.parseInt(reader.readLine());
// Calculate sum
int sum = num1 + num2;
// Send the sum back to the client
writer.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
// Close the connection
clientSocket.close();
System.out.println("Connection closed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}