forked from arduino/TFTP-Bootloader
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathSend.java
More file actions
43 lines (37 loc) · 1.05 KB
/
Send.java
File metadata and controls
43 lines (37 loc) · 1.05 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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Send {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("Please specify send address and port");
System.out.println("Send <addr> <port>");
System.exit(1);
}
try {
InetAddress address = InetAddress.getByName(args[0]);
int port = Integer.parseInt(args[1]);
send(address, port);
} catch(UnknownHostException e) {
e.printStackTrace();
}
}
public static void send(InetAddress _addr, int _port) {
try {
byte[] magic = { 'a', 'r', 'd', 'u', 'i', 'n', 'o' };
DatagramPacket packet;
packet = new DatagramPacket(magic, magic.length, _addr, _port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
System.out.println("Packet sent.");
} catch(SocketException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}