Selected Reading
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 |
|---|---|
| 1 | DatagramPacket(byte[ ] buf, int length) It constructs a DatagramPacket for receiving packets of length length. |
| 2 | DatagramPacket(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. |
| 3 | DatagramPacket(byte[ ] buf, int offset, int length) It constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer. |
| 4 | DatagramPacket(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. |
| 5 | DatagramPacket(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. |
| 6 | DatagramPacket(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 |
|---|---|
| 1 | InetAddress getAddress() This method returns the IP address of the machine to which this datagram is being sent or from which the datagram was received. |
| 2 | byte[ ] getData() This method returns the data buffer. |
| 3 | int getLength() This method returns the length of the data to be sent or the length of the data received. |
| 4 | int getOffset() This method returns the offset of the data to be sent or the offset of the data received. |
| 5 | int 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. |
| 6 | SocketAddress 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. |
| 7 | void setAddress(InetAddress iaddr) This method sets the IP address of the machine to which this datagram is being sent. |
| 8 | void setData(byte[] buf) This method sets the data buffer for this packet. |
| 9 | void setData(byte[ ] buf, int offset, int length) This method sets the data buffer for this packet. |
| 10 | void setLength(int length) This method sets the length for this packet. |
| 11 | void setPort(int iport) This method sets the port number on the remote host to which this datagram is being sent. |
| 12 | void 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