By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

UDP protocol & Object

P: n/a
I need little help with Java and UDP protocol.

Can someone give me an example of how can I send a object by UDP protocol.
Only usefull method is

DatagramPacket(byte[] buf, int length, InetAddress address, int port)

but how to send object ( Data ) through UDP.
Thanks.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"SpitFire" <ma***@net.hrREMOVENOSPAM> wrote in message news:<bs**********@bagan.srce.hr>...
I need little help with Java and UDP protocol.

Can someone give me an example of how can I send a object by UDP protocol.
Only usefull method is

DatagramPacket(byte[] buf, int length, InetAddress address, int port)

but how to send object ( Data ) through UDP.
Thanks.


Serialize the object. That's what object serialization was designed for.
http://www.acm.org/crossroads/xrds4-2/serial.html

---
Jared Dykstra
http://www.bork.org/~jared
Jul 17 '05 #2

P: n/a
"SpitFire" <ma***@net.hrREMOVENOSPAM> wrote in message news:<bs**********@bagan.srce.hr>...
I need little help with Java and UDP protocol.

Can someone give me an example of how can I send a object by UDP protocol.
Only usefull method is

DatagramPacket(byte[] buf, int length, InetAddress address, int port)

but how to send object ( Data ) through UDP.
Thanks.


Easy. First, create a DatagramSocket:

DatagramSocket udpSocket = new DatagramSocket();

Then, use the send() method to send a DatagramPacket on this socket:

udpSocket.send(new DatagramPacket(buf, buf.length, address, port));

You probably want to wait for a response if the recipient is expected to respond:

byte[] retBuf = new byte[1024];
DatagramPacket response = new DatagramPacket(retBuf, retBuf.length);
udpSocket.receive(response);

Check out the Java tutorial for more details:
http://java.sun.com/docs/books/tutor...ing/index.html
Jul 17 '05 #3

P: n/a

"Jared Dykstra" <dy******@hotmail.com> wrote:
Serialize the object. That's what object serialization was designed for.
http://www.acm.org/crossroads/xrds4-2/serial.html


It's working :-)
Thanks
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.