-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatManager.java
More file actions
47 lines (36 loc) · 1.03 KB
/
Copy pathChatManager.java
File metadata and controls
47 lines (36 loc) · 1.03 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
package turing;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.concurrent.LinkedBlockingQueue;
public class ChatManager implements Runnable {
private MulticastSocket ms;
public static LinkedBlockingQueue<String> messages = new LinkedBlockingQueue<String>();
public ChatManager(MulticastSocket ms){
this.ms = ms;
}
@Override
public void run() {
while(true){
try
{
byte[] buffer = new byte[Integer.BYTES];
DatagramPacket len = new DatagramPacket(buffer,buffer.length);
ms.receive(len);
int l = (ByteBuffer.wrap(len.getData()).getInt());
buffer = new byte[l];
DatagramPacket p = new DatagramPacket(buffer,buffer.length);
ms.receive(p);
String message = new String(p.getData());
//System.out.println("Ricevuto messaggio ->"+ message);
messages.add(message);
} catch (SocketException e) {
break;
} catch (IOException e) {
break;
}
}
}
}