Packages  This Package  Prev  Next  Index  

§4.3 Class DatagramSocket

public  class  java.net.DatagramSocket
    extends  java.lang.Object  (I-§1.12)
{
        // Constructors
    public DatagramSocket();	§4.3.1
    public DatagramSocket(int  port);	§4.3.2

        // Methods
    public void close();	§4.3.3
    protected void finalize();	§4.3.4
    public int getLocalPort();	§4.3.5
    public void receive(DatagramPacket  p);	§4.3.6
    public void send(DatagramPacket  p);	§4.3.7
}
This class represents a socket for sending and receiving datagram packets (§4.2).

A datagram socket is the sending or receiving point for a connectionless packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from a machine to another may be routed differently, and may arrive in any order.


Constructors

DatagramSocket

public DatagramSocket() throws SocketException
Constructs a datagram socket and binds it to any available port on the local host machine.
Throws
SocketException (I-§4.17)
if the socket could not be opened, or the socket could not bind the specified local port.

DatagramSocket

public DatagramSocket(int port) throws SocketException
Constructs a datagram socket and binds it to the specified port on the local host machine.
Parameters:
local - port to use
Throws
SocketException (I-§4.17)
if the socket could not be opened, or the socket could not bind the specified local port.

Methods

close

public void close()
Closes this datagram socket.

finalize

protected void finalize()
Ensures that this socket is closed if there are no longer any references to this socket.
Overrides:
finalize in class Object (I-§1.12.4).

getLocalPort

public int getLocalPort()
Returns:
the port number on the local host to which this socket is bound.

receive

public void receive(DatagramPacket p) throws IOException
Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine.
This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the buffer length, the message is truncated.
Parameters:
p - the DatagramPacket into which to place the incoming data
Throws
IOException (I-§2.29)
If an I/O error occurs.

send

public void send(DatagramPacket p) throws IOException
Sends a datagram packet from this socket. The DatagramPacket (I-§4.2) includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host.

Parameters:
p - the DatagramPacket to be sent
Throws
IOException (I-§2.29)
If an I/O error occurs.

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com