简体   繁体   中英

Data not being read by ServerSocket

I'm trying to work on multiple AVD in Android and sending data between them using Sockets.

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();

Client side code:

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();

The problem is that The objects that are sent from one AVD (as given by log) is not being received at other AVD. This happens sometimes and not always at a same point. Any hints as to what could be the problem???

use oos.flush() at the end in client.

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");

阅读后关闭流,否则将假定它仍处于打开状态

oos.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM