forked from arduino/TFTP-Bootloader
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathRecv.java
More file actions
38 lines (33 loc) · 983 Bytes
/
Recv.java
File metadata and controls
38 lines (33 loc) · 983 Bytes
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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class Recv {
public static void main(String[] args) {
if(args.length != 1) {
System.out.println("Please specify receive port");
System.out.println("Recv <port>");
System.exit(1);
}
int port = Integer.parseInt(args[0]);
recv(port);
}
public static void recv(int port) {
try {
byte[] buf = new byte[256];
DatagramSocket socket = new DatagramSocket(port);
DatagramPacket pack = new DatagramPacket(buf, buf.length);
System.out.println("Listening on UDP port 5555.");
System.out.println("Waiting for packets...");
socket.receive(pack);
// display response
String received = new String(pack.getData(), 0, pack.getLength());
System.out.println("Received: " + received);
socket.close();
} catch(SocketException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}