服务端启动
1 public class ServerDemo { 2 //crc 校验 3 private static CrcUtil demo; 4 //str 转字节 5 private static StringToByteutil strToByte; 6 private static Connection conn = null; 7 private SimpleDateFormat formatter = new SimpleDateFormat( 8 "yyyy-MM-dd HH:mm:ss"); 9 static String driver = "com.mysql.jdbc.Driver"; 10 // URL指向要访问的数据库名****** 11 //static String url = "jdbc:mysql://123.57.89.35:3306/hhxficdz_schema"; 12 static String url = "jdbc:mysql://localhost:3306/hhxficdz_schema"; 13 // MySQL配置时的用户名 14 static String user = "root"; 15 // Java连接MySQL配置时的密码****** 16 //static String password = "icdz123"; 17 static String password = "123456"; 18 //crc校验 19 private static CrcUtil crc; 20 private static Connection getconn() { 21 //驱动程序名//不固定,根据驱动 22 try { 23 Class.forName(driver); 24 conn = DriverManager.getConnection(url, user, password); 25 } catch (SQLException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } catch (ClassNotFoundException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 // System.out.println(props.getProperty("mysql.connection.driver_class")); 33 return conn; 34 } 35 /** 36 * 4800服务端口 线程2秒接受 : 37 * 接受一段指令 38 * ---》查询数据库 39 * -----》修改数据库 40 * -------》返回指令 41 * @param args 42 * @throws IOException 43 */ 44 45 46 public static void main(String args[]) throws IOException { 47 48 //为了简单起见,所有的异常信息都往外抛 49 int port = 4800; 50 //定义一个ServerSocket监听在端口8899上 51 ServerSocket server = new ServerSocket(port); 52 while (true) { 53 //server尝试接收其他Socket的连接请求,server的accept方法是阻塞式的 54 Socket socket = server.accept(); 55 //每接收到一个Socket就建立一个新的线程来处理它 56 new Thread(new Task(socket)).start(); 57 } 58 59 }
/** * 用来处理Socket请求的 */ static class Task implements Runnable { private Socket socket; public Task(Socket socket) { this.socket = socket; } public void run() { try { handleSocket(); } catch (Exception e) { e.printStackTrace(); } } /** * 跟客户端Socket进行通信 * @throws Exception */ public void handleSocket() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); StringBuilder sb = new StringBuilder(); String temp; int index; int flag=1; while ((temp=br.readLine()) != null) { System.out.println(temp); if ((index = temp.indexOf("eof")) != -1) {//遇到eof时就结束接收 sb.append(temp.substring(0, index)); break; } sb.append(temp); } System.out.println("from client: " + sb);
客户端发送指令
1 public class ClientDemo { 2 3 4 5 6 7 public static void main(String args[]) throws Exception { 8 //为了简单起见,所有的异常都直接往外抛 9 String host = "127.0.0.1"; //要连接的服务端IP地址 10 int port = 4800; //要连接的服务端对应的监听端口 11 //与服务端建立连接 12 Socket client = new Socket(host, port); 13 //建立连接后就可以往服务端写数据了 14 Writer writer = new OutputStreamWriter(client.getOutputStream()); 15 writer.write("FF01037400014AA856"); 16 writer.write("eof "); 17 writer.flush(); 18 //写完以后进行读操作 19 BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); 20 StringBuffer sb = new StringBuffer(); 21 String temp; 22 int index; 23 while ((temp=br.readLine()) != null) { 24 if ((index = temp.indexOf("eof")) != -1) { 25 sb.append(temp.substring(0, index)); 26 break; 27 } 28 sb.append(temp); 29 } 30 System.out.println("from server: " + sb); 31 writer.close(); 32 br.close(); 33 client.close(); 34 } 35 }
string数组转byte字节
1 public class CrcTest { 2 private static CrcUtil demo; 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 String testStr = "FF060374000101"; 8 9 String str = "FF060374000101";//字符串 10 int m=str.length()/2; 11 if(m*2<str.length()){ 12 m++; 13 } 14 String[] strs=new String[m]; 15 int j=0; 16 for(int i=0;i<str.length();i++){ 17 if(i%2==0){//每隔两个 18 strs[j]=""+str.charAt(i); 19 }else{ 20 strs[j]="0x"+strs[j]+str.charAt(i);//将字符加上两个空格 21 j++; 22 } 23 } 24 25 // 26 byte b2[]=new byte[7]; 27 for (int i = 0; i < 7; i++) { 28 b2[i]=(byte) Integer.parseInt(strs[i].substring(2),16); 29 } 30 31 System.out.println(Arrays.toString(strs)); 32 System.out.println(String.valueOf(Integer.toHexString(demo.CRC16(b2,7))).toUpperCase()); 33 34 35 // 可以用 StringBuilder 这个类,里面有一个接口replace,如下 36 StringBuilder sb = new StringBuilder("abcd"); 37 sb.replace(1, 2, "测试是否替换指定的第二个元素"); 38 System.out.println(sb.toString()); 39 40 //String 替换指定位置的字符 41 String s="02"; 42 s=s.replaceFirst(""+s.charAt(1),"7") ; 43 System.out.println(s); 44 } 45 46 }
1 /** 2 * 3 * string 转 字节 4 *FF060374000101 5 *[0xFF, 0x06, 0x03, 0x74, 0x00, 0x01, 0x01] 6 */ 7 public class StringToByteutil { 8 9 private static CrcUtil demo; 10 //静态方法 11 static byte[] stringToByte(String leftstr) { 12 // TODO Auto-generated method stub 13 int m=leftstr.length()/2; 14 if(m*2<leftstr.length()){ 15 m++; 16 } 17 String[] strs=new String[m]; 18 int j=0; 19 for(int i=0;i<leftstr.length();i++){ 20 if(i%2==0){//每隔两个 21 strs[j]=""+leftstr.charAt(i); 22 }else{ 23 strs[j]="0x"+strs[j]+leftstr.charAt(i);//将字符加上两个空格 24 j++; 25 } 26 } 27 byte b2[]=new byte[m]; 28 for (int i = 0; i < m; i++) { 29 b2[i]=(byte) Integer.parseInt(strs[i].substring(2),16); 30 } 31 32 return b2; 33 } 34 35 public static void main(String[] args) { 36 //字节数组转换校验码 37 System.out.println(String.valueOf(Integer.toHexString(demo.CRC16(stringToByte("FF060374000101"),7))).toUpperCase()); 38 } 39 }
crc校验
1 public class CrcUtil { 2 3 4 public static void main(String[] args) throws IOException { 5 String testStr = "FF060374000101"; 6 byte b1[]={(byte) 0xff,0x06,0x03,0x74,0x00,0x01,0x01}; 7 System.out.println(Integer.toHexString(CRC16(b1,7))); 8 } 9 10 11 static byte[] auchCRCHi = new byte[]{ 12 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, 13 (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, 14 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, 15 (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, 16 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, 17 (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, 18 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, 19 (byte)0x81, (byte)0x40, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, 20 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, 21 (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, 22 (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, 23 (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, 24 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, 25 (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, 26 (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, 27 (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, 28 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, 29 (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, 30 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, 31 (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, 32 (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, 33 (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, 34 (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, 35 (byte)0x81, (byte)0x40, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, 36 (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40, (byte)0x01, (byte)0xC0, (byte)0x80, (byte)0x41, (byte)0x01, (byte)0xC0, 37 (byte)0x80, (byte)0x41, (byte)0x00, (byte)0xC1, (byte)0x81, (byte)0x40 38 }; 39 40 static byte[] auchCRCLo = new byte[]{ 41 (byte)0x00, (byte)0xC0, (byte)0xC1, (byte)0x01, (byte)0xC3, (byte)0x03, (byte)0x02, (byte)0xC2, (byte)0xC6, (byte)0x06, 42 (byte)0x07, (byte)0xC7, (byte)0x05, (byte)0xC5, (byte)0xC4, (byte)0x04, (byte)0xCC, (byte)0x0C, (byte)0x0D, (byte)0xCD, 43 (byte)0x0F, (byte)0xCF, (byte)0xCE, (byte)0x0E, (byte)0x0A, (byte)0xCA, (byte)0xCB, (byte)0x0B, (byte)0xC9, (byte)0x09, 44 (byte)0x08, (byte)0xC8, (byte)0xD8, (byte)0x18, (byte)0x19, (byte)0xD9, (byte)0x1B, (byte)0xDB, (byte)0xDA, (byte)0x1A, 45 (byte)0x1E, (byte)0xDE, (byte)0xDF, (byte)0x1F, (byte)0xDD, (byte)0x1D, (byte)0x1C, (byte)0xDC, (byte)0x14, (byte)0xD4, 46 (byte)0xD5, (byte)0x15, (byte)0xD7, (byte)0x17, (byte)0x16, (byte)0xD6, (byte)0xD2, (byte)0x12, (byte)0x13, (byte)0xD3, 47 (byte)0x11, (byte)0xD1, (byte)0xD0, (byte)0x10, (byte)0xF0, (byte)0x30, (byte)0x31, (byte)0xF1, (byte)0x33, (byte)0xF3, 48 (byte)0xF2, (byte)0x32, (byte)0x36, (byte)0xF6, (byte)0xF7, (byte)0x37, (byte)0xF5, (byte)0x35, (byte)0x34, (byte)0xF4, 49 (byte)0x3C, (byte)0xFC, (byte)0xFD, (byte)0x3D, (byte)0xFF, (byte)0x3F, (byte)0x3E, (byte)0xFE, (byte)0xFA, (byte)0x3A, 50 (byte)0x3B, (byte)0xFB, (byte)0x39, (byte)0xF9, (byte)0xF8, (byte)0x38, (byte)0x28, (byte)0xE8, (byte)0xE9, (byte)0x29, 51 (byte)0xEB, (byte)0x2B, (byte)0x2A, (byte)0xEA, (byte)0xEE, (byte)0x2E, (byte)0x2F, (byte)0xEF, (byte)0x2D, (byte)0xED, 52 (byte)0xEC, (byte)0x2C, (byte)0xE4, (byte)0x24, (byte)0x25, (byte)0xE5, (byte)0x27, (byte)0xE7, (byte)0xE6, (byte)0x26, 53 (byte)0x22, (byte)0xE2, (byte)0xE3, (byte)0x23, (byte)0xE1, (byte)0x21, (byte)0x20, (byte)0xE0, (byte)0xA0, (byte)0x60, 54 (byte)0x61, (byte)0xA1, (byte)0x63, (byte)0xA3, (byte)0xA2, (byte)0x62, (byte)0x66, (byte)0xA6, (byte)0xA7, (byte)0x67, 55 (byte)0xA5, (byte)0x65, (byte)0x64, (byte)0xA4, (byte)0x6C, (byte)0xAC, (byte)0xAD, (byte)0x6D, (byte)0xAF, (byte)0x6F, 56 (byte)0x6E, (byte)0xAE, (byte)0xAA, (byte)0x6A, (byte)0x6B, (byte)0xAB, (byte)0x69, (byte)0xA9, (byte)0xA8, (byte)0x68, 57 (byte)0x78, (byte)0xB8, (byte)0xB9, (byte)0x79, (byte)0xBB, (byte)0x7B, (byte)0x7A, (byte)0xBA, (byte)0xBE, (byte)0x7E, 58 (byte)0x7F, (byte)0xBF, (byte)0x7D, (byte)0xBD, (byte)0xBC, (byte)0x7C, (byte)0xB4, (byte)0x74, (byte)0x75, (byte)0xB5, 59 (byte)0x77, (byte)0xB7, (byte)0xB6, (byte)0x76, (byte)0x72, (byte)0xB2, (byte)0xB3, (byte)0x73, (byte)0xB1, (byte)0x71, 60 (byte)0x70, (byte)0xB0, (byte)0x50, (byte)0x90, (byte)0x91, (byte)0x51, (byte)0x93, (byte)0x53, (byte)0x52, (byte)0x92, 61 (byte)0x96, (byte)0x56, (byte)0x57, (byte)0x97, (byte)0x55, (byte)0x95, (byte)0x94, (byte)0x54, (byte)0x9C, (byte)0x5C, 62 (byte)0x5D, (byte)0x9D, (byte)0x5F, (byte)0x9F, (byte)0x9E, (byte)0x5E, (byte)0x5A, (byte)0x9A, (byte)0x9B, (byte)0x5B, 63 (byte)0x99, (byte)0x59, (byte)0x58, (byte)0x98, (byte)0x88, (byte)0x48, (byte)0x49, (byte)0x89, (byte)0x4B, (byte)0x8B, 64 (byte)0x8A, (byte)0x4A, (byte)0x4E, (byte)0x8E, (byte)0x8F, (byte)0x4F, (byte)0x8D, (byte)0x4D, (byte)0x4C, (byte)0x8C, 65 (byte)0x44, (byte)0x84, (byte)0x85, (byte)0x45, (byte)0x87, (byte)0x47, (byte)0x46, (byte)0x86, (byte)0x82, (byte)0x42, 66 (byte)0x43, (byte)0x83, (byte)0x41, (byte)0x81, (byte)0x80, (byte)0x40 67 }; 68 Socket socket = null; 69 70 71 static int CRC16(byte[] puchMsg, int len) 72 { 73 int ucCRCHi = (byte)0xFF ; /* ??CRC×??ú3?ê??ˉ */ 74 int ucCRCLo = (byte)0xFF ; /* μíCRC ×??ú3?ê??ˉ */ 75 int iIndex ; /* CRC?-?·?Dμ??÷òy */ /* ′?ê????¢?o3??? */ 76 for (int i = 0; i < len; ++i) { 77 iIndex = (ucCRCLo ^ puchMsg[i]) & 0x00ff; 78 ucCRCLo = ucCRCHi ^ auchCRCHi[iIndex]; 79 ucCRCHi = auchCRCLo[iIndex]; 80 } 81 return ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff; 82 } 83 84 85 86 87 88 89 90 91 92 private short CRCCheck(byte[] dat,int num ) 93 { 94 short CRCSum=0; 95 96 return CRCSum; 97 } 98 99 /** 100 * 读取数据 101 * 102 * @param in 103 * @return 104 */ 105 public String doRead(InputStream in) { 106 // 引用关系,不要在此处关闭流 107 String aa = ""; 108 try { 109 byte[] bytes = new byte[in.available()]; 110 in.read(bytes); 111 aa = new String(bytes).trim(); 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } 115 116 return aa; 117 } 118 119 /** 120 * 写入数据 121 * 122 * @param out 123 * @return 124 */ 125 public boolean doWrite(OutputStream out, String resStr) { 126 // 引用关系,不要在此处关闭流 127 try { 128 out.write(resStr.getBytes()); 129 out.flush(); 130 } catch (IOException e) { 131 // TODO Auto-generated catch block 132 e.printStackTrace(); 133 } 134 return true; 135 } 136 /** 137 * byte数组转int 138 * @param src 139 * @param offset 140 * @return 141 */ 142 public static int bytesToInt(byte[] b) { 143 int mask=0xff; 144 int temp=0; 145 int n=0; 146 for(int i=0;i<b.length;i++){ 147 n<<=8; 148 temp=b[i]&mask; 149 n|=temp; 150 } 151 return n; 152 } 153 /** 154 * int转byte数组 155 * @param value 156 * @return 157 */ 158 public static byte[] intToBytes(int value) 159 { 160 byte[] byte_src = new byte[4]; 161 byte_src[3] = (byte) ((value & 0xFF000000)>>24); 162 byte_src[2] = (byte) ((value & 0x00FF0000)>>16); 163 byte_src[1] = (byte) ((value & 0x0000FF00)>>8); 164 byte_src[0] = (byte) ((value & 0x000000FF)); 165 return byte_src; 166 } 167 /** 168 * double转byte数组 169 * @param d 170 * @return 171 */ 172 public static byte[] doubleToBytes(double d) 173 { 174 byte writeBuffer[]= new byte[4]; 175 long v = Double.doubleToLongBits(d); 176 writeBuffer[0] = (byte)(v >>> 24); 177 writeBuffer[1] = (byte)(v >>> 16); 178 writeBuffer[2] = (byte)(v >>> 8); 179 writeBuffer[3] = (byte)(v >>> 0); 180 return writeBuffer; } 181 182 /** 183 * byte数组转double 184 * @param readBuffer 185 * @return 186 */ 187 public static double bytesToDouble(byte[] b) { 188 long l; 189 l = b[0]; 190 l &= 0xff; 191 l |= ((long) b[1] << 8); 192 l &= 0xffff; 193 l |= ((long) b[2] << 16); 194 l &= 0xffffff; 195 l |= ((long) b[3] << 24); 196 l &= 0xffffffffl; 197 return Double.longBitsToDouble(l); 198 } 199 200 /** 201 * byte数组转float 202 * @param b 203 * @param index 204 * @return 205 */ 206 public static float getFloat(byte[] b) { 207 int l; 208 l = b[3]; 209 l &= 0xff; 210 l |= ((long) b[2] << 8); 211 l &= 0xffff; 212 l |= ((long) b[1] << 16); 213 l &= 0xffffff; 214 l |= ((long) b[0] << 24); 215 return Float.intBitsToFloat(l); 216 } 217 /** 218 * float转byte数组 219 * @param p 220 * @return 221 */ 222 private byte[] GetBytes(float p) { 223 byte[] data = new byte[4]; 224 int ivalue = Float.floatToIntBits(p); 225 226 data[0] = (byte)(ivalue >> 24); 227 data[1] = (byte)(ivalue >> 16); 228 data[2] = (byte)(ivalue >> 8); 229 data[3] = (byte) ivalue; 230 return data; // TODO Auto-generated method stub 231 } 232 233 234 235 }