writing an object to socket
I want to write a program in which I have to pass some data and packet_no of that data...
So, I am creating an class Packet
. I want to send the an Packet
object through the OutputStream
of the Socket.
How do I a开发者_如何学Gochieve this?
ThanksThe thing, that you need, is called an ObjectOutputStream. it is created from the regular OutputStream. At the reciever side you will need the appropriate ObjectInputStream. Your object and all of it's fields must implement Serializable interface.
Example code:
OutputStream os = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(p);
精彩评论