Client.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class Client implements Runnable {
private String ip;
private int port;
private String message;
public Client() {
}
public Client(String ip, int port, String message) {
super();
this.ip = ip;
this.port = port;
this.message = message;
}
public void run() {
try {
DatagramSocket client = new DatagramSocket();
byte[] messageBytes = Util.hexStringToBytes(message);
InetSocketAddress socketAddress = new InetSocketAddress(ip, port);
DatagramPacket sendData = new DatagramPacket(messageBytes,
messageBytes.length);
DatagramPacket receiveData = new DatagramPacket(new byte[25], 25);
sendData.setSocketAddress(socketAddress);
System.out.println("send:" + message);
client.send(sendData);
client.receive(receiveData);
byte[] receiveMessage = receiveData.getData();
System.out.println("received:"
+ Util.bytesToHexString(receiveMessage));
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Util.java
public class Util {
/**
* Convert byte[] to hex string.这里我们可以将byte转换成int,
* 然后利用Integer.toHexString(int)来转换成16进制字符串。
*
* @param src
* byte[] data
* @return hex string
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString().toUpperCase();
}
/**
* Convert byte to hex string
*
* @param src
* @return
*/
public static String byteToHexString(byte src) {
StringBuilder stringBuilder = new StringBuilder("");
int v = src & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
return stringBuilder.toString().toUpperCase();
}
/**
* Convert hex string to byte[]
*
* @param hexString
* the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* Convert char to byte
*
* @param c
* char
* @return byte
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 获取校验码
*
* @param hexStr
* @return
*/
public static String getChecksum(String hexStr) {
byte[] message = hexStringToBytes(hexStr);
byte temp = message[0];
for (int i = 1; i < message.length; i++) {
temp = (byte) (temp ^ message[i]);
}
return byteToHexString(temp);
}
}
3.Main.java
public class Main {
public static void main(String[] args) throws InterruptedException {
String ip = "127.0.0.1";
int port = 9999;
String messageFlag = "7E";
for (int i = 100; i < 999; i++) {
String messageBody = "F55509000018710438" + i;
String checksum = Util.getChecksum(messageBody);
String message = messageFlag + messageBody + checksum + messageFlag;
new Thread(new Client(ip, port, message)).start();
}
}
}