Java.net.DatagramPacket Class



Introduction

The java.net.DatagramPacket class represents a datagram packet. They are used to implement a connectionless packet delivery service.

Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.

Class declaration

Following is the declaration for java.net.DatagramPacket class:

public final class DatagramPacket
    extends Object

Class constructors

S.N.Constructor
1DatagramPacket(byte[ ] buf, int length)
It constructs a DatagramPacket for receiving packets of length length.
2DatagramPacket(byte[ ] buf, int length, InetAddress address, int port)
It constructs a datagram packet for sending packets of length length to the specified port number on the specified host.
3DatagramPacket(byte[ ] buf, int offset, int length)
It constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer.
4DatagramPacket(byte[ ] buf, int offset, int length, InetAddress address, int port)
It constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host.
5DatagramPacket(byte[ ] buf, int offset, int length, SocketAddress address)
It constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host.
6DatagramPacket(byte[ ] buf, int length, SocketAddress address)
It constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

Class methods

S.N.Method & Description
1InetAddress getAddress()
This method returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.
2byte[ ] getData()
This method returns the data buffer.
3int getLength()
This method returns the length of the data to be sent or the length of the data received.
4int getOffset()
This method returns the offset of the data to be sent or the offset of the data received.
5int getPort()
This method returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.
6SocketAddress getSocketAddress()
This method gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.
7void setAddress(InetAddress iaddr)
This method sets the IP address of the machine to which this datagram is being sent.
8void setData(byte[] buf)
This method sets the data buffer for this packet.
9void setData(byte[ ] buf, int offset, int length)
This method sets the data buffer for this packet.
10void setLength(int length)
This method sets the length for this packet.
11void setPort(int iport)
This method sets the port number on the remote host to which this datagram is being sent.
12void setSocketAddress(SocketAddress address)
This method sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.
Advertisements