Skip to content

Instantly share code, notes, and snippets.

@akirad
Last active August 7, 2017 02:07
Show Gist options
  • Save akirad/e600846f9abe7b577b6c32d587d9fcc2 to your computer and use it in GitHub Desktop.
Save akirad/e600846f9abe7b577b6c32d587d9fcc2 to your computer and use it in GitHub Desktop.
A Java program which sends udp data.
// A sample to send a udp data.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.File;
public class sendUdpData {
static private int destPort;
static private InetAddress destHost;
static private String sendDataHexStr;
static private final String USAGE = "Usage: java sendUdpData -host <target host> -port <target port> {-data <send data> | -file <file path>}";
public static void main(String[] args) {
try {
// Set default values.
destPort = 162;
destHost = InetAddress.getByName("localhost");
sendDataHexStr = "0102030405";
setArgs(args);
// Create packet data.
byte sendData[] = hexStrToBin(sendDataHexStr);
DatagramPacket dataPacket = new DatagramPacket(sendData, sendData.length, destHost, destPort);
// Send trap.
DatagramSocket dataSocket = new DatagramSocket();
dataSocket.send(dataPacket);
dataSocket.close();
}
catch (Exception e) {
System.err.println("Error happened as follows.");
System.err.println(e.getMessage());
e.printStackTrace();
System.err.println(USAGE);
}
}
private static void setArgs(String[] args) {
// Ex. -dest 127.0.0.1 -port 162 -data 0102030405
for (int i=0; i< args.length; i++) {
if ("-host".equals(args[i])) {
try {
destHost = InetAddress.getByName(args[++i]);
} catch (UnknownHostException e) {
System.err.println("The specified host is unknown. " + USAGE);
System.exit(1);
}
}
if ("-port".equals(args[i])) {
try {
destPort = Integer.parseInt(args[++i]);
} catch (Exception e) {
System.err.println("The specified port is invalid. " + USAGE);
System.exit(1);
}
}
if ("-file".equals(args[i])) {
File inputFile = new File(args[++i]);
if (!inputFile.isFile()) {
System.err.println(args[i] + " is not a file.");
System.exit(1);
}
sendDataHexStr = removeSpaces(getFileContents(inputFile));
}
// If both "-file" and "-data" are specified, "-data" is used.
if ("-data".equals(args[i])) {
sendDataHexStr = args[++i];
}
}
}
private static String removeSpaces(String string) {
StringBuilder stringWithoutSpace = new StringBuilder();
for (int i=0; i<string.length(); i++) {
if (string.charAt(i) == ' ') {
continue;
}
stringWithoutSpace.append(string.charAt(i));
}
return stringWithoutSpace.toString();
}
private static String getFileContents(File file) {
StringBuilder fileContents = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
fileContents.append(line);
}
} catch (IOException e) {
System.err.println("Can't read " + file.getAbsolutePath());
System.exit(1);
}
return fileContents.toString();
}
private static byte[] hexStrToBin(String hexStr) {
byte[] bin = new byte[hexStr.length() / 2];
try {
for (int i = 0; i < bin.length; i++) {
// Change a hex string to a numeric value(binary value).
// Ex. "3f" (‭0011 0011 ‭0110 0110‬‬) -> 63 (0011 1111)
bin[i] = (byte) Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
}
} catch (NumberFormatException e) {
System.err.println("Send data is invalid. " + USAGE);
System.exit(1);
}
return bin;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment