This repository was archived by the owner on Oct 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
73 lines (57 loc) · 2.33 KB
/
Server.java
File metadata and controls
73 lines (57 loc) · 2.33 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SERVER UDP
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
final DatagramSocket socket;
final int PORT = 3000;
byte[] buffer = new byte[1000];
// creazione socket
try {
socket = new DatagramSocket(PORT);
System.out.println("Creato server: " + socket.getLocalAddress() + " porta " + socket.getLocalPort());
} catch (SocketException e) {
System.out.println("Errore creazione socket");
return;
}
while (true) {
try {
// ricevi
DatagramPacket rec = receive(socket, buffer);
System.out.println("Ricevuto da (" + rec.getAddress() + " porta " + rec.getPort() + "):\n" + getMessage(rec));
// rispondi
send(socket, buffer, rec.getAddress(), rec.getPort(), "ACK");
System.out.println("Risposto a (" + rec.getAddress() + " porta " + rec.getPort() + "):\nACK");
System.out.println("---");
} catch (IOException e) {
System.out.println("Errore nello scambio di messaggi");
}
}
}
/**
* Riceve un pacchetto sulla @param socket, scrivendo nel @param buffer
* @return il pacchetto ricevuto
* @throws IOException
*/
public static DatagramPacket receive(DatagramSocket socket, byte[] buffer) throws IOException {
final DatagramPacket received = new DatagramPacket(buffer, buffer.length);
socket.receive(received);
return received;
}
/**
* Estrae la stringa dal pacchetto @param received
* @return la stringa estratta
*/
public static String getMessage(DatagramPacket received) {
return new String(received.getData(), 0, received.getLength());
}
/**
* Invia un pacchetto all'indirizzo @param address, porta @param port conentente come messaggio @param message attraverso la @param socket con @param buffer
* @throws IOException
*/
public static void send(DatagramSocket socket, byte[] buffer, InetAddress address, int port, String message) throws IOException {
buffer = message.getBytes();
DatagramPacket toSend = new DatagramPacket(buffer, buffer.length, address, port);
socket.send(toSend);
}
}