-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
51 lines (35 loc) · 1.3 KB
/
Server.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
//Riccardo Pirani www.riccardopirani.altervista.org
import java.io.*;
import java.net.*;
public class Server{
static final int PORT = 1024;
public static void main(String args[]){
try{
ServerSocket ss=new ServerSocket(PORT);
while(true) {
Socket ns = ss.accept();
BufferedReader networkIn = new BufferedReader(new InputStreamReader(ns.getInputStream()));
PrintWriter networkOut=new PrintWriter(ns.getOutputStream());
String line;
System.out.println("\n Attendo valori \n ");
while ((line = networkIn.readLine()) != null) {
System.out.println("Ricevuto: " + line);
try{
int number=Integer.parseInt(line);
number=number*number;
networkOut.println(number);
networkOut.flush();
}
catch(Exception ex){
System.out.println("\n Non mi hai inviato un numero \n");
}
}
ns.close();
}
}
catch(Exception ex){
System.out.println("\n Errore Server: "+ex);
System.exit(2);
}
}
}