在javaweb的socket通信中,由于要发送一些指令报文,涉及到十六进制数据,这里测试修改了一个好用的转换函数,程序如下
放到websocket的服务器端程序里进行的效果测试
import org.jetbrains.annotations.Contract; import java.io.IOException; import java.util.concurrent.CopyOnWriteArraySet; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; //该注解是一个类层次的注解,功能是将目前的类定义成一个websocket服务器端,注解的值将 //被用于监听用户连接的终端访问URL地址。 @ServerEndpoint("/WebSocketBroadcast") public class WebSocketBroadcast { //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 private static int onlineCount = 0; //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识 private static CopyOnWriteArraySet<WebSocketBroadcast> websocketset = new CopyOnWriteArraySet<WebSocketBroadcast>(); //与某个客户端的连接会话,需要通过它来给客户端发送数据 private Session session; //---------------------------------------------- //测试字符串拆分成字节流的方法,已成功 2018.12.18 public String str = "7e ff ff ff 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f"; private byte[] byte_str = new byte[50]; private int j; public byte char2byte(char c) { String digital = "0123456789ABCDEF"; return (byte)digital.indexOf(c); } public byte[] str2byte(String hexstring) /* hex2char-> [0]7 [1]E [2]F [3]F [4]F [5]F [6]F [7]F [8]0 [9]1 [10]0 [11]2 [12]0 [13]3 [14]0 [15]4 [16]0 [17]5 [18]0 [19]6 [20]0 [21]7 [22]0 [23]8 [24]0 [25]9 [26]0 [27]A [28]0 [29]B [30]0 [31]C [32]0 [33]D [34]0 [35]E [36]0 [37]F [0]0x7e [1]0xff [2]0xff [3]0xff [4]0x01 [5]0x02 [6]0x03 [7]0x04 [8]0x05 [9]0x06 [10]0x07 [11]0x08 [12]0x09 [13]0x0a [14]0x0b [15]0x0c [16]0x0d [17]0x0e [18]0x0f [0]0x7e [1]0xff [2]0xff [3]0xff [4]0x01 [5]0x02 [6]0x03 [7]0x04 [8]0x05 [9]0x06 [10]0x07 [11]0x08 [12]0x09 [13]0x0a [14]0x0b [15]0x0c [16]0x0d [17]0x0e [18]0x0f */ { int j; //去掉hex中的所有空格 String hex_no_space = hexstring.replace(" ", ""); //字符串中的字符都改为大写,因为后面需要在digital中找到对应的字符 hex_no_space = hex_no_space.toUpperCase(); char[] hex2char = hex_no_space.toCharArray(); int length = hex_no_space.length()/2; byte[] d = new byte[length]; System.out.println("hex2char->"); for(j=0;j<(hex2char.length);j++) { System.out.printf("[%d]%c ", j, hex2char[j]); //[0]7 [1]e [2]f [3]f ... } System.out.println("\r\n"); for(int i=0;i<length;i++) { int pos = i*2; d[i] = (byte)(char2byte(hex2char[pos]) << 4 | char2byte(hex2char[pos + 1])); System.out.printf("[%d]0x%02x ", i, d[i]); } return d; } @OnOpen public void onOpen(Session session) { this.session = session; websocketset.add(this); addOnlineCount(); System.out.println("有新连接加入!当前在线人数为 " + getOnlineCount()); byte_str = str2byte(str); System.out.println("\r\n"); for(int i=0;i<byte_str.length;i++) { System.out.printf("[%d]0x%02x ", i, byte_str[i]); } System.out.println("\r\n"); System.out.println("\r\n"); } @OnClose public void onClose() { websocketset.remove(this); subOnlineCount(); System.out.println("有一个连接关闭!当前在线人数为 " + getOnlineCount()); } //收到客户端发来的消息 @OnMessage public void onMessage(String message, Session session) throws IOException, InterruptedException { System.out.println("收到来自客户端的消息: " + message); } public void sendMessage(String message) throws IOException { this.session.getBasicRemote().sendText(message); } @Contract(pure = true) public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { WebSocketBroadcast.onlineCount++; } public static synchronized void subOnlineCount() { WebSocketBroadcast.onlineCount--; } }