-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpcontrol.java
56 lines (50 loc) · 1.82 KB
/
udpcontrol.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
52
53
54
55
56
import java.io.IOException;
import java.net.*;
import java.io.*;
public class udpcontrol {
public static InetSocketAddress addr = new InetSocketAddress("0.0.0.0",9999);
public static DatagramSocket datagramSocket;
public boolean enviando=false;
public void escuchaservidor() {
try {
while(enviando)
{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
byte[] mensaje;
datagramSocket = new DatagramSocket(addr);
mensaje = new byte[64];
DatagramPacket datagrama1 = new DatagramPacket(mensaje,64);
datagramSocket.receive(datagrama1);
System.out.println("Mensaje entrante de : "+datagrama1.getAddress().toString());
System.out.println("-> "+new String(mensaje));
datagramSocket.close();
} catch (Exception e) {
}
}
public void enviar() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Completa la ip: 172.30.");
String ip="172.30."+br.readLine();
System.out.print("Mensaje: ");
String mensaje2=br.readLine();
enviando=true;
datagramSocket.close();
DatagramSocket datagramSocket2 = new DatagramSocket(addr);
InetAddress addr2 = InetAddress.getByName(ip);
DatagramPacket datagrama2 = new DatagramPacket(mensaje2.getBytes(),mensaje2.getBytes().length, addr2, 9999);
datagramSocket2.send(datagrama2);
System.out.println("Enviado a 172.30."+ip+": "+mensaje2);
System.out.println("");
datagramSocket2.close();
enviando=false;
notifyAll();
} catch (Exception e) {
}
}
}