How to send Object over Datagram Socket
android, datagram, java, sockets
Solution
Layer an ObjectOutputStream on top of a ByteArrayOutputStream on the sending side. Gather the bytes from the ByteArrayOutputStream (after the write), and send that in your datagram packet. Do the reverse on the receiving side to unpack the data back into an Object.
Pseudocode for your sending side:
final ByteArrayOutputStream baos = new ByteArrayOutputStream(6400);
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
final byte[] data = baos.toByteArray();
final DatagramPacket packet = new DatagramPacket(data, data.length);
// Send the packet
Problem
I am working on some Android Project & trying to pass object over Datagram Socket to some another device Object contains 'String' Data Members of the class ( UserName,Services) .. How can I do that??