我尝试在Android中使用多个AVD并使用套接字在它们之间发送数据。
Serverside code-snippet:
ServerSocket ss = new ServerSocket(10000);
Log.v("ReceiverTask", "Receiver waiting for requests");
connectedSocket = ss.accept();
ObjectInputStream ois = new ObjectInputStream(connectedSocket.getInputStream());
Object obj = ois.readObject();
ois.close();
ss.close();
客户端代码:
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),njr.sendTo());
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(njr);
Log.d("Client","Object send successfull");
oos.flush();
bos.flush();
oos.reset();
oos.close();
bos.close();
socket.close();
问题在于,在其他AVD上没有接收到从一个AVD发送的对象(由日志给出)。这有时会发生,并不总是在同一点上。关于可能出现什么问题的任何提示???
答案 0 :(得分:0)
在客户端最后使用oos.flush()
。
socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),njr.sendTo());
bos = new BufferedOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(bos);
oos.writeObject(njr);
oos.flush();
oos.close();
Log.d("Client","Object send successfull");
答案 1 :(得分:0)
阅读后关闭流,否则会认为它仍然打开
oos.Close();