Socket長連線,位元組傳送
阿新 • • 發佈:2018-12-22
LogKit.error("IP連線"); Socket socket = new Socket("192.168.1.172", 5001); OutputStream outputStream = socket.getOutputStream(); PrintStream dos = new PrintStream(outputStream); byte[] bsa=new byte[1024]; bsa[0]=(byte) 0xFF; bsa[1]=(byte) 0x00; bsa[2]=(byte) 0x08; bsa[3]=(byte) 0x63; bsa[4]=(byte) 0x91; bsa[5]=(byte) 0x89; bsa[6]=(byte) 0x76; bsa[8]=(byte) 0x18; bsa[18]=(byte) 0xFB; dos.write(bsa); byte[] b = new byte[1024];while(true) { InputStream inputStream = socket.getInputStream(); inputStream.read(b); StringBuilder ipStr = new StringBuilder(); for (byte bs : b) { if (bs < 0) { ipStr.append("." + (bs & 0xFF)); }else if (bs == 0) { continue; } else { ipStr.append("." + (int) bs); } } String ip1 = ipStr.toString(); ip1 = ip1.substring(1); System.out.println(ip1); } }