我正在尝试通过android模拟器中的套接字发送对象。 每个仿真器都将对象发送给自身和另外两个仿真器。 侦听和发送对象位于两个不同的端口和线程中。 它在发送第一个数据包后得到了java.io.StreamCorruptedException。 在某些情况下,发送到其他仿真器的对象会到达,但发送给自身的对象在发送第一个对象后永远不会到达自身。 我检查了我的对象发送和接收代码与他人,他们告诉他们没有看到任何问题,但我确实得到了一致的异常。 这篇文章是最后的手段。请帮忙!
这是我的服务器主题,它一直在监听 -
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();
}
decision()方法产生一个新线程。因此,调用上述方法时不会产生延迟。
这是在同一个程序中,但完全不同的线程
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();
}
我不断收到java.io.StreamCorruptedException。
我已经尝试过,关闭,刷新以及gogle建议的内容。但我仍然得到这个错误。
以下是错误日志的摘录 -
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
答案 0 :(得分:0)
解决问题 - 我已经将objectinputstream和objectoutputstream声明为私有,并使其成为全局的。这以某种方式破坏了流。 通过使它们都是本地的,错误被消除了。