Hoser
Hoser

Reputation: 5044

DatagramPacket can receive data?

I'm reading through my textbook and I see this:

The first constructor:

public Datagrampacket (byte ibuf [], int ilength)

constructs a DatagramPacket for receiving packets of length ilength.

Is this just an odd wording, or do DatagramPacket's actually receive data along with sending it? I always thought DatagramPackets were just classes containing information you would send between DatagramSockets

Upvotes: 0

Views: 143

Answers (2)

mjshaw
mjshaw

Reputation: 401

DatagramPacket does not send or receive data. Instead, it is used by by DataSocket in two ways.

  • It is used by DatagramSocket.receive(DatagramPacket packet) which populates packet with some received data,
  • or it is used by DatagramSocket.send(DatagramPacket packet) to send the data contained in packet.

Hope this helps.

Upvotes: 1

user207421
user207421

Reputation: 310893

Check the Javadoc. DatagramPackets are used for both sending and receiving. See DatagramSocket.receive().

Upvotes: 0

Related Questions