-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComunicationSystem.java
46 lines (35 loc) · 1.39 KB
/
ComunicationSystem.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
package project;
import java.net.*;
class ComunicacionSystem {
private DatagramSocket DatagramSockSend;
private DatagramSocket DatagramSockReceive;
public void ComunicationSystem(){
}
public void Send(String message, String Destino, int port){
try {
DatagramPacket paqueteUDP;
InetAddress dir_remota = InetAddress.getByName( Destino );
byte[] buffer = message.getBytes(); // retorna los bytes del string
paqueteUDP = new DatagramPacket(buffer, buffer.length, dir_remota, port);
DatagramSockSend = new DatagramSocket();
DatagramSockSend.send(paqueteUDP);
}
catch(Exception e){
}
}
public String Receive(int port){
int longitdmensaje = 100;
byte pkg_byte[] = new byte[1];
try {
byte buffer[] = new byte[longitdmensaje]; //buffer solo para el DatagramPacket
DatagramPacket dp = new DatagramPacket( buffer, longitdmensaje);
DatagramSockReceive = new DatagramSocket(port);
DatagramSockReceive.receive(dp); //lee los datos
pkg_byte = new byte[dp.getLength()]; //array de bytes para los datos
pkg_byte = dp.getData(); // asigna los bytes de datos
}
catch(Exception e){
}
return new String(pkg_byte);
}
}