package main;import java.io.DataInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class JavaSocketApplication {public static void main(String[] args) throws Exception {JavaSocketApplication receiver = new JavaSocketApplication(8700);receiver.receive();}private final ServerSocket serverSocket;public JavaSocketApplication(int port) throws IOException {serverSocket = new ServerSocket(port);}private void receive() throws IOException {System.out.println("等待客户端连接...");Socket socket = serverSocket.accept();try (DataInputStream dis = new DataInputStream(socket.getInputStream())) {byte[] bytes = new byte[1024]; // 假设发送的字节数不超过 1024 个int size = dis.read(bytes); // size 是读取到的字节数String hex = bytesToHex(bytes, 0, size);System.out.println("接收到的byte数组的十六进制:" + hex);}}/*** 将 byte 数组转化为十六进制字符串** @param bytes byte[] 数组* @param begin 起始位置* @param end 结束位置* @return byte 数组的十六进制字符串表示*/private String bytesToHex(byte[] bytes, int begin, int end) {StringBuilder hexBuilder = new StringBuilder(2 * (end - begin));for (int i = begin; i < end; i++) {hexBuilder.append(Character.forDigit((bytes[i] & 0xF0) >> 4, 16)); // 转化高四位hexBuilder.append(Character.forDigit((bytes[i] & 0x0F) >> 0, 16)); // 转化低四位hexBuilder.append(' '); // 加一个空格将每个字节分隔开}return hexBuilder.toString().toUpperCase();}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。