java.net
Class DatagramPacket
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.
DatagramPacket(byte[] buf, int length) - Initializes a new instance of
DatagramPacket for
receiving packets from 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, InetAddress address, int port) - 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.
|
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
|
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
DatagramPacket
public DatagramPacket(byte[] buf,
int length)
Initializes a new instance of DatagramPacket
for
receiving packets from the network.
buf
- A buffer for storing the returned packet datalength
- The length of the buffer (must be <= buf.length)
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.
buf
- The buffer for holding the incoming datagram.offset
- The offset into the buffer to start writing.length
- The maximum number of bytes to read.
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.
buf
- A buffer containing the data to sendoffset
- The offset into the buffer to start writing from.length
- The length of the buffer (must be <= buf.length)address
- The address to send toport
- The port to send to
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.
buf
- A buffer containing the data to sendoffset
- The offset into the buffer to start writing from.length
- The length of the buffer (must be <= buf.length)address
- The socket address to send to
DatagramPacket
public DatagramPacket(byte[] buf,
int length,
InetAddress address,
int port)
Initializes a new instance of DatagramPacket
for
transmitting packets across the network.
buf
- A buffer containing the data to sendlength
- The length of the buffer (must be <= buf.length)address
- The address to send toport
- The port to send to
DatagramPacket
public DatagramPacket(byte[] buf,
int length,
SocketAddress address)
throws SocketException
Initializes a new instance of DatagramPacket
for
transmitting packets across the network.
buf
- A buffer containing the data to sendlength
- The length of the buffer (must be <= buf.length)address
- The socket address to send to
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
.
- The address for this packet.
getData
public byte[] getData()
Returns the data buffer for this packet
- This packet's data buffer
getLength
public int getLength()
Returns the length of the data in the buffer
getOffset
public int getOffset()
This method returns the current offset value into the data buffer
where data will be sent from.
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.
- The port number for this packet
getSocketAddress
public SocketAddress getSocketAddress()
Gets the socket address of the host this packet
will be sent to/is coming from
- The socket address of the remote host
setAddress
public void setAddress(InetAddress address)
This sets the address to which the data packet will be transmitted.
address
- The destination address
setData
public void setData(byte[] buf)
Sets the data buffer for this packet.
buf
- The new buffer for this packet
setData
public void setData(byte[] buf,
int offset,
int length)
This method sets the data buffer for the packet.
buf
- The byte array containing the data for this packet.offset
- The offset into the buffer to start reading data from.length
- The number of bytes of data in the buffer.
setLength
public void setLength(int length)
Sets the length of the data in the buffer.
length
- The new length. (Where len <= buf.length)
setPort
public void setPort(int port)
This sets the port to which the data packet will be transmitted.
port
- The destination port
DatagramPacket.java -- Class to model a packet to be sent via UDP
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.