|
You have to writeObject first.
A sample code as below, hope it's helpful. ----------------------------------------
try {
String strSocketInput = ;
ByteArrayInputStream baIn = new ByteArrayInputStream(strSocketInput.getBytes());
DataInputStream oInputStream = new DataInputStream(baIn);
byte[] arr = new byte[oInputStream.available()];
oInputStream.read(arr);
baIn.close();
oInputStream.close();
String strIdentifier = ; byte[] identifier = strIdentifier.getBytes();
byte[] ob = new byte[arr.length + identifier.length];
ByteArrayInputStream baNew = new ByteArrayInputStream(arr);
baNew.read(ob, 0, arr.length);
baNew = new ByteArrayInputStream(identifier);
baNew.read(ob, arr.length, identifier.length);
ByteArrayOutputStream baOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(baOut);
objOut.writeObject(ob);
objOut.flush();
objOut.close();
byte[] baSend = baOut.toByteArray();
baOut.close();
ByteArrayInputStream baRecIn = new ByteArrayInputStream(baSend);
ObjectInputStream objIn = new ObjectInputStream(baRecIn);
byte[] baRec = (byte[])objIn.readObject();
String strReceive = new String(baRec);
System.out.println( + strReceive);
} catch (Exception e) {
e.printStackTrace();
} |
|
转载于:https://www.cnblogs.com/cnsoft/archive/2004/10/29/58003.html