当前位置:网站首页>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;
}
}
边栏推荐
- Implementation of queue
- 目标检测——使用yolov6进行视频推理
- 力扣每日一题-第30天-594.最长和谐子序列
- Small program large screen adaptation Guide
- The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail
- Sum of digits under k-ary representation of leetcode simple problem
- 开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
- VerilogA——动态比较器
- 2022.02.14 - 239. A single element in an ordered array
- Mongodb sort function
猜你喜欢

关于DDNS

Easy to understand TCP four waves (multi picture explanation)

Servlet version conflict causes page 404

Linux Installation redis

Creating a new generation of production and service tools with robot education

Leetcode simple problem building arrays with stack operation

二叉树的迭代法前序遍历的两种方法

Browser local storage

Observer mode vs publish subscribe mode

Agile invincible event
随机推荐
What are the uses of wireless pressure collectors?
Meta metauniverse female safety problems occur frequently. How to solve the relevant problems in the metauniverse?
After “Go to Definition”, is there a command to return to where you came from?
Week 12 - task 2- shoulder to shoulder cadres
JDBC | Chapter 6: simple use of database connection pool
jetson tx2
Maximum ascending subarray sum of leetcode simple problem
Baidu applet automatically submits search
Li Kou daily question - day 30 -1281 Difference of sum of bit product of integer
Linux Installation redis
Output of character pointer to string in C language
Design and practice of kubernetes cluster and application monitoring scheme
Servlet version conflict causes page 404
Venn diagram proportional and color shading with semi transparency
Character pointer as function parameter
Two houses with different colors and the farthest distance
力扣每日一题-第30天-1281.整数的各位积和之差
关于端口转发程序的一点思考
Client and server working modes of JVM
Where is the Gcov symbol- Where are the gcov symbols?