1. 將字符串轉化成字節,並進行ASCII編碼數組
Entity.getDeviceName().getBytes("US-ASCII");
2. Java拷貝byte數組網絡
System.arraycopy(Recvtemp, DeviceNameLen + 1 + timeStampLen + 1 + 1 + FileNameLen +1 + 1, Entity.getFileByteArr(), 0, FileLen);
3. Java發送byte數組編碼
byte[] byteArray = serializeMessage.Serializer(message); Socket s = new Socket("192.168.7.37", 8885); OutputStream os = s.getOutputStream(); os.write(byteArray);
4. 網絡操做必須開新的線程,不可以在主線程裏操做線程
new Thread(){ public void run(){ try{ sendHeartBeat(); } catch (Exception e) { e.printStackTrace(); } } }.start();