img
Other methods give you access to various attributes associated with a DatagramSocket.
Here is a sampling:
InetAddress getInetAddress( )
If the socket is connected, then the address is returned.
Other wise, null is returned.
int getLocalPor t( )
Returns the number of the local por t.
int getPor t( )
Returns the number of the por t to which the socket is
connected. It returns ­1 if the socket is not connected
to a por t.
boolean isBound( )
Returns true if the socket is bound to an address.
Returns false other wise.
boolean isConnected( )
Returns true if the socket is connected to a ser ver.
Returns false other wise.
void setSoTimeout(int millis)
Sets the time-out period to the number of milliseconds
passed in millis.
throws SocketException
DatagramPacket
DatagramPacket defines several constructors. Four are shown here:
DatagramPacket(byte data[ ], int size)
DatagramPacket(byte data[ ], int offset, int size)
DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port)
DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int port)
The first constructor specifies a buffer that will receive data and the size of a packet. It is
used for receiving data over a DatagramSocket. The second form allows you to specify an
offset into the buffer at which data will be stored. The third form specifies a target address
and port, which are used by a DatagramSocket to determine where the data in the packet
will be sent. The fourth form transmits packets beginning at the specified offset into the
data. Think of the first two forms as building an "in box," and the second two forms as
stuffing and addressing an envelope.
DatagramPacket defines several methods, including those shown here, that give access
to the address and port number of a packet, as well as the raw data and its length. In general,
the get methods are used on packets that are received and the set methods are used on
packets that will be sent.
InetAddress getAddress( )
Returns the address of the source (for datagrams
being received) or destination (for datagrams
being sent).
byte[ ] getData( )
Returns the byte array of data contained in the
datagram. Mostly used to retrieve data from the
datagram after it has been received.
int getLength( )
Returns the length of the valid data contained in
the byte array that would be returned from the
getData( ) method. This may not equal the length
of the whole byte array.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home