Re: UDP protocol & Object

From: Karl von Laudermann (karl_at_ueidaq.com)
Date: 12/30/03


Date: 30 Dec 2003 08:10:58 -0800


"SpitFire" <max00@net.hrREMOVENOSPAM> wrote in message news:<bsni83$jc4$1@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/tutorial/networking/index.html



Relevant Pages

  • Re: UDP protocol & Object
    ... > 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. ... That's what object serialization was designed for. ... Jared Dykstra ...
    (comp.lang.java)
  • UDP protocol & Object
    ... 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(bytebuf, int length, InetAddress address, int port) ...
    (comp.lang.java)