开发者

writing an object to socket

I have got a problem in here...!

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?

Thanks


The 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜