-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulticast servidor
More file actions
55 lines (47 loc) · 1.49 KB
/
Copy pathMulticast servidor
File metadata and controls
55 lines (47 loc) · 1.49 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
public class ManejoMulticast
{
private MulticastSocket socket_multicast;
private DatagramPacket datagrama;
private byte [] dato;
private InetAddress grupo;
public ManejoMulticast()
{
try {
socket_multicast = new MulticastSocket();
dato = new byte[0];
grupo = InetAddress.getByName("231.0.0.1");
datagrama = new DatagramPacket(dato, 0, grupo, 10000);
} catch (IOException ex) {
System.out.println("error al crear el socket de multicast");
}
}
/**
* metodo recibe un mensaje que se quiere mandar por el datagrama
* con él modifica el datagrama y retorna true cuando lo envie
* y false si no lo envía
* @param mensajeAMandar
* @return
*/
public boolean enviarMensajeMulticast(String mensajeAMandar)
{
if(mensajeAMandar.isEmpty())
{
System.out.println("el mensaje estaba vacio");
return false;
}else
{
try
{
byte [] temporal = mensajeAMandar.getBytes();
datagrama.setData(temporal);
datagrama.setLength(temporal.length);
socket_multicast.send(datagrama);
return true;
}catch(IOException ioe)
{
System.out.println("error al mandar el datagrama");
return false;
}
}
}
}