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

Quick Search    Search Deep

java.net
Class DatagramSocket  view DatagramSocket download DatagramSocket.java

java.lang.Object
  extended byjava.net.DatagramSocket
Direct Known Subclasses:
MulticastSocket

public class DatagramSocket
extends java.lang.Object

This class models a connectionless datagram socket that sends individual packets of data across the network. In the TCP/IP world, this means UDP. Datagram packets do not have guaranteed delivery, or any guarantee about the order the data will be received on the remote host.


Field Summary
private  boolean bound
          True if socket is bound.
private static DatagramSocketImplFactory factory
          This is the user DatagramSocketImplFactory for this class.
private  DatagramSocketImpl impl
          This is the implementation object used by this socket.
private  boolean implCreated
          True if socket implementation was created.
private  InetAddress remoteAddress
          This is the address we are "connected" to
private  int remotePort
          This is the port we are "connected" to
 
Constructor Summary
  DatagramSocket()
          Initializes a new instance of DatagramSocket that binds to a random port and every address on the local machine.
protected DatagramSocket(DatagramSocketImpl impl)
          Creates a DatagramSocket from a specified DatagramSocketImpl instance
  DatagramSocket(int port)
          Initializes a new instance of DatagramSocket that binds to the specified port and every address on the local machine.
  DatagramSocket(int port, InetAddress addr)
          Initializes a new instance of DatagramSocket that binds to the specified local port and address.
  DatagramSocket(SocketAddress address)
          Initializes a new instance of DatagramSocket that binds to the specified local port and address.
 
Method Summary
 void bind(SocketAddress address)
          Binds the socket to the given socket address.
 void close()
          Closes this datagram socket.
 void connect(InetAddress address, int port)
          This method connects this socket to the specified address and port.
 void connect(SocketAddress address)
          Connects the datagram socket to a specified socket address.
 void disconnect()
          This method disconnects this socket from the address/port it was connected to.
 boolean getBroadcast()
          Checks if SO_BROADCAST is enabled
 java.nio.channels.DatagramChannel getChannel()
          Returns the datagram channel assoziated with this datagram socket.
(package private)  DatagramSocketImpl getImpl()
           
 InetAddress getInetAddress()
          This method returns the remote address to which this socket is connected.
 InetAddress getLocalAddress()
          Returns the local address this datagram socket is bound to.
 int getLocalPort()
          Returns the local port this socket is bound to.
 SocketAddress getLocalSocketAddress()
          Returns the local SocketAddress this socket is bound to.
 int getPort()
          This method returns the remote port to which this socket is connected.
 int getReceiveBufferSize()
          This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.
 SocketAddress getRemoteSocketAddress()
          Returns the SocketAddress of the host this socket is conneted to or null if this socket is not connected.
 boolean getReuseAddress()
          Checks if SO_REUSEADDR is enabled.
 int getSendBufferSize()
          This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.
 int getSoTimeout()
          Returns the value of the socket's SO_TIMEOUT setting.
 int getTrafficClass()
          Returns the current traffic class
 boolean isBound()
          Returns the binding state of the socket.
 boolean isClosed()
          Checks if the datagram socket is closed.
 boolean isConnected()
          Returns the connection state of the socket.
 void receive(DatagramPacket p)
          Reads a datagram packet from the socket.
 void send(DatagramPacket p)
          Sends the specified packet.
 void setBroadcast(boolean enable)
          Enables/Disables SO_BROADCAST
static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
          Sets the datagram socket implementation factory for the application
 void setReceiveBufferSize(int size)
          This method sets the value for the system level socket option SO_RCVBUF to the specified value.
 void setReuseAddress(boolean on)
          Enables/Disables SO_REUSEADDR.
 void setSendBufferSize(int size)
          This method sets the value for the system level socket option SO_SNDBUF to the specified value.
 void setSoTimeout(int timeout)
          Sets the value of the socket's SO_TIMEOUT value.
 void setTrafficClass(int tc)
          Sets the traffic class value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factory

private static DatagramSocketImplFactory factory
This is the user DatagramSocketImplFactory for this class. If this variable is null, a default factory is used.


impl

private DatagramSocketImpl impl
This is the implementation object used by this socket.


implCreated

private boolean implCreated
True if socket implementation was created.


remoteAddress

private InetAddress remoteAddress
This is the address we are "connected" to


remotePort

private int remotePort
This is the port we are "connected" to


bound

private boolean bound
True if socket is bound.

Constructor Detail

DatagramSocket

protected DatagramSocket(DatagramSocketImpl impl)
Creates a DatagramSocket from a specified DatagramSocketImpl instance

Since:
1.4

DatagramSocket

public DatagramSocket()
               throws SocketException
Initializes a new instance of DatagramSocket that binds to a random port and every address on the local machine.


DatagramSocket

public DatagramSocket(int port)
               throws SocketException
Initializes a new instance of DatagramSocket that binds to the specified port and every address on the local machine.


DatagramSocket

public DatagramSocket(int port,
                      InetAddress addr)
               throws SocketException
Initializes a new instance of DatagramSocket that binds to the specified local port and address.


DatagramSocket

public DatagramSocket(SocketAddress address)
               throws SocketException
Initializes a new instance of DatagramSocket that binds to the specified local port and address.

Since:
1.4
Method Detail

getImpl

DatagramSocketImpl getImpl()
                     throws SocketException

close

public void close()
Closes this datagram socket.


getInetAddress

public InetAddress getInetAddress()
This method returns the remote address to which this socket is connected. If this socket is not connected, then this method will return null.

Since:
1.2

getPort

public int getPort()
This method returns the remote port to which this socket is connected. If this socket is not connected, then this method will return -1.

Since:
1.2

getLocalAddress

public InetAddress getLocalAddress()
Returns the local address this datagram socket is bound to.

Since:
1.1

getLocalPort

public int getLocalPort()
Returns the local port this socket is bound to.


getSoTimeout

public int getSoTimeout()
                 throws SocketException
Returns the value of the socket's SO_TIMEOUT setting. If this method returns 0 then SO_TIMEOUT is disabled.

Since:
1.1

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Sets the value of the socket's SO_TIMEOUT value. A value of 0 will disable SO_TIMEOUT. Any other value is the number of milliseconds a socket read/write will block before timing out.

Since:
1.1

getSendBufferSize

public int getSendBufferSize()
                      throws SocketException
This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.

Since:
1.2

setSendBufferSize

public void setSendBufferSize(int size)
                       throws SocketException
This method sets the value for the system level socket option SO_SNDBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Since:
1.2

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.

Since:
1.2

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
This method sets the value for the system level socket option SO_RCVBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Since:
1.2

connect

public void connect(InetAddress address,
                    int port)
This method connects this socket to the specified address and port. When a datagram socket is connected, it will only send or receive packets to and from the host to which it is connected. A multicast socket that is connected may only send and not receive packets.

Since:
1.2

disconnect

public void disconnect()
This method disconnects this socket from the address/port it was connected to. If the socket was not connected in the first place, this method does nothing.

Since:
1.2

receive

public void receive(DatagramPacket p)
             throws java.io.IOException
Reads a datagram packet from the socket. Note that this method will block until a packet is received from the network. On return, the passed in DatagramPacket is populated with the data received and all the other information about the packet.


send

public void send(DatagramPacket p)
          throws java.io.IOException
Sends the specified packet. The host and port to which the packet are to be sent should be set inside the packet.


bind

public void bind(SocketAddress address)
          throws SocketException
Binds the socket to the given socket address.

Since:
1.4

isClosed

public boolean isClosed()
Checks if the datagram socket is closed.

Since:
1.4

getChannel

public java.nio.channels.DatagramChannel getChannel()
Returns the datagram channel assoziated with this datagram socket.

Since:
1.4

connect

public void connect(SocketAddress address)
             throws SocketException
Connects the datagram socket to a specified socket address.

Since:
1.4

isBound

public boolean isBound()
Returns the binding state of the socket.

Since:
1.4

isConnected

public boolean isConnected()
Returns the connection state of the socket.

Since:
1.4

getRemoteSocketAddress

public SocketAddress getRemoteSocketAddress()
Returns the SocketAddress of the host this socket is conneted to or null if this socket is not connected.

Since:
1.4

getLocalSocketAddress

public SocketAddress getLocalSocketAddress()
Returns the local SocketAddress this socket is bound to.

Since:
1.4

setReuseAddress

public void setReuseAddress(boolean on)
                     throws SocketException
Enables/Disables SO_REUSEADDR.

Since:
1.4

getReuseAddress

public boolean getReuseAddress()
                        throws SocketException
Checks if SO_REUSEADDR is enabled.

Since:
1.4

setBroadcast

public void setBroadcast(boolean enable)
                  throws SocketException
Enables/Disables SO_BROADCAST

Since:
1.4

getBroadcast

public boolean getBroadcast()
                     throws SocketException
Checks if SO_BROADCAST is enabled

Since:
1.4

setTrafficClass

public void setTrafficClass(int tc)
                     throws SocketException
Sets the traffic class value

Since:
1.4

getTrafficClass

public int getTrafficClass()
                    throws SocketException
Returns the current traffic class

Since:
1.4

setDatagramSocketImplFactory

public static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
                                         throws java.io.IOException
Sets the datagram socket implementation factory for the application