-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPServer.java
70 lines (62 loc) · 2.66 KB
/
TCPServer.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.*;
import java.net.*;
//64050037
class TCPServer {
/**
* @param argv
*/
public static void main(String argv[]) {
String clientInput;
String[] numbers;
String capitalizedSentence;
ServerSocket welcomeSocket = null;
Socket connectionSocket = null;
BufferedReader inFromClient = null;
DataOutputStream outToClient = null;
try {
welcomeSocket = new ServerSocket(1667);
System.out.println("Server: Waiting for client connection at port number 1667");
} catch (IOException e) {
System.out.println("Cannot create a welcome socket");
System.exit(1);
}
while (true) {
try {
connectionSocket = welcomeSocket.accept();
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.print("Client: enter two numbers separated by space (to end just press enter): ");
while (true) {
clientInput = inFromClient.readLine();
if (clientInput == null || clientInput.equals("END")) {
System.out.println("Server: Connection closed by the client.");
break;
}
numbers = clientInput.split(" ");
if (numbers.length != 2) {
outToClient.writeBytes("Invalid input. Please enter two numbers separated by space.\n");
continue;
}
int num1 = Integer.parseInt(numbers[0]);
int num2 = Integer.parseInt(numbers[1]);
int result = num1 + num2;
capitalizedSentence = "The result is " + result + "\n";
outToClient.writeBytes(capitalizedSentence);
}
} catch (IOException | NumberFormatException e) {
System.out.println("Error processing the connection: " + e.getMessage());
} finally {
try {
if (inFromClient != null)
inFromClient.close();
if (outToClient != null)
outToClient.close();
if (connectionSocket != null)
connectionSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}