import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest {
private ServerSocket ss;
private Socket socket;
PrintWriter out;
private int i = 0;
public ServerTest() {
try {
ss = new ServerSocket(7838);
while (true) {
System.out.println(0);
socket = ss.accept();
ss.setSoTimeout(50000);
byte[] b = new byte[1024];
b[0] = (byte) 0x55;
b[1] = (byte) 0xAA;
b[2] = (byte) 0x01;
b[3] = (byte) 0x00;
b[4] = (byte) 0x00;
b[5] = (byte) 0x00;
b[6] = (byte) 0x00;
b[7] = (byte) 0x00;
b[8] = (byte) 0x00;
b[9] = (byte) 0x00;
b[10] = (byte) 0x00;
b[11] = (byte) 0x00;
b[12] = (byte) 0x00;
b[13] = (byte) 0x00;
b[14] = (byte) 0x00;
b[15] = (byte) 0x00;
b[16] = (byte) 0x00;
InputStream socketReader = socket.getInputStream();
OutputStream socketWriter = socket.getOutputStream();
socketWriter.write(b);
System.out.println("OK");
socketWriter.flush();
i = i + 1;
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerTest();
}
}
上面是我百度的JAVA socket 发送十六进制数据的代码,通过这个思路,我进行了一些抽象:
因为我需要模拟多个报文,每个报文都用字节流拆分比较费时间,从网上查询到把一个字符串直接拆分成字节流的方法:
public class socketWriter {
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
// toUpperCase将字符串中的所有字符转换为大写
hexString = hexString.toUp

本文介绍了使用JAVA进行socket报文交互的步骤,包括读取协议配置、计算包体长度、建立连接、发送接收数据及解析响应。同时,探讨了基于该方法的压力测试,用于对协议进行长时间、多线程的测试,监测性能和响应时间。此外,还提到了一款通用自动化测试工具,具备高级语言API、动态脚本执行、多平台远程控制和并行测试能力。

588

被折叠的 条评论
为什么被折叠?



