forked from ConnetionClientServer/IntegerSum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
51 lines (41 loc) · 1.71 KB
/
Server.java
File metadata and controls
51 lines (41 loc) · 1.71 KB
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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class Server {
public static void main(String[] args) {
String operator;
int number, number2;
int total;
try {
ServerSocket service = new ServerSocket(9000);
service.setSoTimeout(10000);
System.out.println("Apro la connessione sulla porta 9000");
Socket socket = service.accept();
System.out.println("Un client si è connesso!");
BufferedReader byClient = new BufferedReader (new InputStreamReader(socket.getInputStream()));
BufferedWriter toClient = new BufferedWriter (new OutputStreamWriter(socket.getOutputStream()));
toClient.write("Dammi i numeri da sommare!! Adesso!");
toClient.flush();
operator = byClient.readLine();
System.out.println("E' stato inserito l'operatore: " + operator);
number = Integer.parseInt(byClient.readLine());
System.out.println("E' stato inserito il 1° numero: " + number);
number2 = Integer.parseInt(byClient.readLine());
System.out.println("E' stato inserito il 2° numero: " + number2);
if(!operator.equals("-") ) {
total = number+number2;
}else
total = number-number2;
System.out.println("Il tuo risultato è: " + number + operator + number2 +"= " + total);
socket.close();
service.close();
}
catch(SocketTimeoutException t){
System.out.println("Tempo scaduto");
}
catch (IOException e) {
System.out.println(e + "ciao ciao");
}
}
}