I am trying to send objects over sockets in android emulators.
Each emulator sends objects to itself and two other emulators.
The listening and sending of the objects are in two different ports and threads.
I get the java.io.StreamCorruptedException after it has sent the first packet.
In some cases the objects sent to the other emulators do reach ,but the object sent to itself never reaches itself after the first object is sent.
I have checked my object sending and receiving code with others, and they have told they see no issues, but i do get the exception consistently.
This post is as a last resort. Please help!
This is my server thread, which keeps listening –
public void StartListening() {
new Thread(){
public void run(){
//packet p = null;
try {
ListenSocket = new ServerSocket(L_PORT_NUM);
packet p= null;
Log.i("init","entering the listen while loop");
while(true){
Socket TempSocket1 = ListenSocket.accept();
ois = new ObjectInputStream(TempSocket1.getInputStream());
p = (packet) ois.readObject();
decide(p);
ois.close();
TempSocket1.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
decide() method spawns a new thread. Therefore no delay is incurred when the above method is called.
This is in the same program but completely different thread
public void myWriteObject(packet m) throws IOException{
final packet temp = m;
//Create a new thread,Since network operations is not advised in main thread.
new Thread(){
public void run() {
Log.i("myWriteObject","--------- ");
int i=NUM_SYS;
int temp_portNum=11108;
while(i!=0){
try {
Socket SendSocket1= new Socket("10.0.2.2",temp_portNum);
oos = new ObjectOutputStream(SendSocket1.getOutputStream());
oos.writeObject(temp);
oos.flush();
oos.close();
SendSocket1.close();
Log.i("myWriteObject","OBJECT SENT TO -"+temp_portNum);
i--;
temp_portNum=temp_portNum+4;
Thread.sleep(1000);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
I keep getting java.io.StreamCorruptedException.
I have tried, closing, flushing and whatnots advised in gogle. But I still get this error.
Here is a snippet of the error log-
03-17 19:18:23.361: I/CLock(int n)(1638): clock initialized0 0 0
03-17 19:18:23.370: I/init(1638): entered oncreate
03-17 19:18:23.620: I/init(1638): entering the listen while loop
03-17 19:18:24.001: D/gralloc_goldfish(1638): Emulator without GPU emulation detected.
03-17 19:18:24.420: I/ActivityManager(100): Displayed project1.DistSys.App/.Project1Activity: +2s526ms
03-17 19:18:24.981: W/NetworkManagementSocketTagger(100): setKernelCountSet(10009, 0) failed with errno -2
03-17 19:18:33.961: I/TEST1(1638): sending to EVERYONE from TEST 1-1 0 0 5554:1
03-17 19:18:33.991: I/myWriteObject(1638): ---------
03-17 19:18:34.102: I/TEST1(1638): sending to EVERYONE from TEST 1-2 0 0 5554:2
03-17 19:18:34.120: I/myWriteObject(1638): ---------
03-17 19:18:34.500: I/myWriteObject(1638): OBJECT SENT TO -11108
03-17 19:18:34.500: I/INCOMING(1638): msg value 5554:0SEQUENCER MSG value 0seq_no value 0get key stamp 5554:0clock 1 0 0
03-17 19:18:34.520: W/System.err(1638): java.io.StreamCorruptedException
03-17 19:18:34.530: I/DECIDE(1638): packets seq_no value is-0 5554:0 5554:0
03-17 19:18:34.550: W/System.err(1638): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1528)
03-17 19:18:34.580: W/System.err(1638): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
03-17 19:18:34.580: W/System.err(1638): at project1.DistSys.App.Project1Activity$9.run(Project1Activity.java:285)
03-17 19:18:34.600: I/DECIDE-CLKSTATUS(1638): INSERT TO DB port-clock-5554 2 0 0
03-17 19:18:34.820: W/System.err(1638): java.io.EOFException
03-17 19:18:34.840: W/System.err(1638): at java.io.DataInputStream.readByte(DataInputStream.java:98)
03-17 19:18:34.850: W/System.err(1638): at java.io.ObjectInputStream.nextTC(ObjectInputStream.java:506)
03-17 19:18:34.850: W/System.err(1638): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:778)
03-17 19:18:34.850: W/System.err(1638): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
03-17 19:18:34.861: W/System.err(1638): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
03-17 19:18:34.861: W/System.err(1638): at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
03-17 19:18:34.880: W/System.err(1638): at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
03-17 19:18:34.890: W/System.err(1638): at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
03-17 19:18:34.977: W/System.err(1638): at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
03-17 19:18:34.977: W/System.err(1638): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1851)
03-17 19:18:34.977: W/System.err(1638): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
03-17 19:18:34.991: W/System.err(1638): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
03-17 19:18:34.991: W/System.err(1638): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
03-17 19:18:35.011: W/System.err(1638): at
Solved the problem-
I had declared objectinputstream and objectoutputstream as private and had made it global. This somehow corrupted the stream.
By making both of them local , the error was eliminated.