当前位置:网站首页>json tobean
json tobean
2022-06-29 06:37:00 【inspire121】














































在这里插入代码片
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import static Code_conversion.bytesToHexString;
public class Receiving_message_Server extends Thread {
public static ArrayList<Socket> sockets = new ArrayList<Socket>();
private Socket_data_service socket_data_service = new Socket_data_serviceimpl();
private Socket server;
private ServerSocket serverSocket;
public Receiving_message_Server() {
}
public Receiving_message_Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
}
public void run() {
while (true) {
try {
System.out.println("等待远程连接.....");
server = serverSocket.accept(); // 阻塞监听端口等待请求连接
sockets.add(server);
System.out.println("远程主机地址:" + server.getRemoteSocketAddress());
System.out.println("连接时间:" + new Date());
InputStream in = server.getInputStream(); // 获取客户端发送的流
BufferedInputStream bis = new BufferedInputStream(in); // 流存放缓冲区
DataOutputStream dos = new DataOutputStream(out); // 获取缓冲区流数据
byte[] bytes = new byte[1]; // 一次读取一个byte
String ret = "";
while (dis.read(bytes) != -1) {
ret += bytesToHexString(bytes) + " "; //调用字节转化16进制字符串方法
if (dis.available() == 0) {
//一个请求
System.out.println("收到报文数据: " + ret);
ret = "";
}
}
in.close();
dos.close();
bis.close();
server.close(); // 关闭连接
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
int port = 8888; // 监听端口
try {
Thread t = new Receiving_message_Server(port); // 线程初始化
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

在这里插入代码片
public class Code_conversion {
/**
* 二进制转换16进制字符串
* @param bytes
* @return
*/
public static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
/***
* 16进制字符串转换二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
边栏推荐
- Hyperledger Fabric 2. X custom smart contract
- Aging design guide for applets
- Is there any difference between a=a+b and a+=b?
- Test Development - ten years of sharpening one sword (VII) interface test tool postman
- package.json的所有配置项及其用法,你都熟悉么
- Flutter is configured with a domestic image and connected to the real machine
- [Flink] flinksql and table programming cases
- Meta metauniverse female safety problems occur frequently. How to solve the relevant problems in the metauniverse?
- Fault: NetBt log for id4321
- How does MySQL implement distributed locks?
猜你喜欢

Presto-Trial

Leetcode simple question: judging the color of a grid on a chess board

配置Flutter开发环境

MySQL learning notes

The most complete machine learning model training process

关于 localStorage 的一些高阶用法

Internet enterprises need CRM software to help

Clickhouse data type

Yyds dry goods inventory meituan's two-sided experience, and finally there was a surprise?

Configuring the flutter development environment
随机推荐
作为一名合格的网工,你必须掌握的 DHCP Snooping 知识!
Redistemplate handles hash integer type problem resolution
Why is there a packaging type?
力扣每日一题-第30天-594.最长和谐子序列
力扣每日一题-第30天-1523.位1的个数
Share 10 interview questions related to JS promise
Go basic data types: characters and strings
Test Development - ten years of sharpening one sword (VII) interface test tool postman
Draw smooth curves - methods needed - drawing smooth curves - methods needed
力扣每日一题-第30天-1281.整数的各位积和之差
Analysis on the wave of learning robot education for children
Ribbon service invocation and load balancing
Stack -- 739 Daily temperature
I would like to ask what securities dealers recommend? Is it safe to open an account online?
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
VerilogA——计数器
String and variable are spliced into variable name
What are the uses of wireless pressure collectors?
Leetcode simple question: judging the color of a grid on a chess board
融入STEAM教育的劳动技能课程