Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.net
Class DatagramPacket  view DatagramPacket download DatagramPacket.java

java.lang.Object
  extended byjava.net.DatagramPacket

public final class DatagramPacket
extends java.lang.Object

This class models a packet of data that is to be sent across the network using a connectionless protocol such as UDP. It contains the data to be send, as well as the destination address and port. Note that datagram packets can arrive in any order and are not guaranteed to be delivered at all.

This class can also be used for receiving data from the network.

Note that for all method below where the buffer length passed by the caller cannot exceed the actually length of the byte array passed as the buffer, if this condition is not true, then the method silently reduces the length value to maximum allowable value. Written using on-line Java Platform 1.2 API Specification, as well as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). Status: Believed complete and correct.


Field Summary
private  InetAddress address
          The address to which the packet should be sent or from which it was received.
private  byte[] buffer
          The data buffer to send
(package private)  int length
          The length of the data buffer to send.
(package private)  int maxlen
          The maximal length of the buffer.
private  int offset
          This is the offset into the buffer to start sending from or receiving to.
private  int port
          The port to which the packet should be sent or from which it was was received.
 
Constructor Summary
DatagramPacket(byte[] buf, int length)
          Initializes a new instance of DatagramPacket for receiving packets from the network.
DatagramPacket(byte[] buf, int length, InetAddress address, int port)
          Initializes a new instance of DatagramPacket for transmitting packets across the network.
DatagramPacket(byte[] buf, int offset, int length)
          This method initializes a new instance of DatagramPacket which has the specified buffer, offset, and length.
DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
          Initializes a new instance of DatagramPacket for transmitting packets across the network.
DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)
          Initializes a new instance of DatagramPacket for transmitting packets across the network.
DatagramPacket(byte[] buf, int length, SocketAddress address)
          Initializes a new instance of DatagramPacket for transmitting packets across the network.
 
Method Summary
 InetAddress getAddress()
          Returns the address that this packet is being sent to or, if it was used to receive a packet, the address that is was received from.
 byte[] getData()
          Returns the data buffer for this packet
 int getLength()
          Returns the length of the data in the buffer
 int getOffset()
          This method returns the current offset value into the data buffer where data will be sent from.
 int getPort()
          Returns the port number this packet is being sent to or, if it was used to receive a packet, the port that it was received from.
 SocketAddress getSocketAddress()
          Gets the socket address of the host this packet will be sent to/is coming from
 void setAddress(InetAddress address)
          This sets the address to which the data packet will be transmitted.
 void setData(byte[] buf)
          Sets the data buffer for this packet.
 void setData(byte[] buf, int offset, int length)
          This method sets the data buffer for the packet.
 void setLength(int length)
          Sets the length of the data in the buffer.
 void setPort(int port)
          This sets the port to which the data packet will be transmitted.
 void setSocketAddress(SocketAddress address)
          Sets the address of the remote host this package will be sent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

private byte[] buffer
The data buffer to send


offset

private int offset
This is the offset into the buffer to start sending from or receiving to.


length

int length
The length of the data buffer to send.


maxlen

int maxlen
The maximal length of the buffer.


address

private InetAddress address
The address to which the packet should be sent or from which it was received.


port

private int port
The port to which the packet should be sent or from which it was was received.

Constructor Detail

DatagramPacket

public DatagramPacket(byte[] buf,
                      int offset,
                      int length)
This method initializes a new instance of DatagramPacket which has the specified buffer, offset, and length.

Since:
1.2

DatagramPacket

public DatagramPacket(byte[] buf,
                      int length)
Initializes a new instance of DatagramPacket for receiving packets from the network.


DatagramPacket

public DatagramPacket(byte[] buf,
                      int offset,
                      int length,
                      InetAddress address,
                      int port)
Initializes a new instance of DatagramPacket for transmitting packets across the network.

Since:
1.2

DatagramPacket

public DatagramPacket(byte[] buf,
                      int length,
                      InetAddress address,
                      int port)
Initializes a new instance of DatagramPacket for transmitting packets across the network.


DatagramPacket

public DatagramPacket(byte[] buf,
                      int offset,
                      int length,
                      SocketAddress address)
               throws SocketException
Initializes a new instance of DatagramPacket for transmitting packets across the network.

Since:
1.4

DatagramPacket

public DatagramPacket(byte[] buf,
                      int length,
                      SocketAddress address)
               throws SocketException
Initializes a new instance of DatagramPacket for transmitting packets across the network.

Since:
1.4
Method Detail

getAddress

public InetAddress getAddress()
Returns the address that this packet is being sent to or, if it was used to receive a packet, the address that is was received from. If the constructor that doesn not take an address was used to create this object and no packet was actually read into this object, then this method returns null.


getPort

public int getPort()
Returns the port number this packet is being sent to or, if it was used to receive a packet, the port that it was received from. If the constructor that doesn not take an address was used to create this object and no packet was actually read into this object, then this method will return 0.


getData

public byte[] getData()
Returns the data buffer for this packet


getOffset

public int getOffset()
This method returns the current offset value into the data buffer where data will be sent from.

Since:
1.2

getLength

public int getLength()
Returns the length of the data in the buffer


setAddress

public void setAddress(InetAddress address)
This sets the address to which the data packet will be transmitted.

Since:
1.1

setPort

public void setPort(int port)
This sets the port to which the data packet will be transmitted.

Since:
1.1

setSocketAddress

public void setSocketAddress(SocketAddress address)
                      throws java.lang.IllegalArgumentException
Sets the address of the remote host this package will be sent

Since:
1.4

getSocketAddress

public SocketAddress getSocketAddress()
Gets the socket address of the host this packet will be sent to/is coming from

Since:
1.4

setData

public void setData(byte[] buf)
Sets the data buffer for this packet.

Since:
1.1

setData

public void setData(byte[] buf,
                    int offset,
                    int length)
This method sets the data buffer for the packet.

Since:
1.2

setLength

public void setLength(int length)
Sets the length of the data in the buffer.

Since:
1.1