java.net
Class PlainDatagramSocketImpl

java.lang.Object
  extended byjava.net.DatagramSocketImpl
      extended byjava.net.PlainDatagramSocketImpl
All Implemented Interfaces:
SocketOptions

class PlainDatagramSocketImpl
extends DatagramSocketImpl

Concrete datagram and multicast socket implementation base class. Note: This is not a public class, so that applets cannot call into the implementation directly and hence cannot bypass the security checks present in the DatagramSocket and MulticastSocket classes.

Author:
Pavani Diwanji

Field Summary
private  boolean connected
           
private  InetAddress connectedAddress
           
private  int connectedPort
           
private  boolean loopbackMode
           
private  int multicastInterface
           
private  int timeout
           
private  int trafficClass
           
private  int ttl
           
 
Fields inherited from class java.net.DatagramSocketImpl
fd, localPort
 
Fields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS, SO_BINDADDR, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
(package private) PlainDatagramSocketImpl()
           
 
Method Summary
protected  void bind(int lport, InetAddress laddr)
          Binds a datagram socket to a local port.
protected  void close()
          Close the socket.
protected  void connect(InetAddress address, int port)
          Connects a datagram socket to a remote destination.
private  void connect0(InetAddress address, int port)
           
protected  void create()
          Creates a datagram socket
private  void datagramSocketClose()
           
private  void datagramSocketCreate()
           
protected  void disconnect()
          Disconnects a previously connected socket.
private  void disconnect0()
           
protected  void finalize()
          Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
 Object getOption(int optID)
          Fetch the value of an option.
protected  int getTimeToLive()
          Get the TTL (time-to-live) option.
protected  byte getTTL()
          Get the TTL (time-to-live) option.
private static void init()
          Perform class load-time initializations.
protected  void join(InetAddress inetaddr)
          Join the multicast group.
private  void join(InetAddress inetaddr, NetworkInterface netIf)
           
protected  void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
          Join the multicast group.
protected  void leave(InetAddress inetaddr)
          Leave the multicast group.
private  void leave(InetAddress inetaddr, NetworkInterface netIf)
           
protected  void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
          Leave the multicast group.
protected  int peek(InetAddress i)
          Peek at the packet to see who it is from.
protected  int peekData(DatagramPacket p)
          Peek at the packet to see who it is from.
protected  void receive(DatagramPacket p)
          Receive the datagram packet.
protected  void send(DatagramPacket p)
          Sends a datagram packet.
 void setOption(int optID, Object o)
          set a value - since we only support (setting) binary options here, o must be a Boolean
protected  void setTimeToLive(int ttl)
          Set the TTL (time-to-live) option.
protected  void setTTL(byte ttl)
          Set the TTL (time-to-live) option.
private  Object socketGetOption(int opt)
           
private  void socketSetOption(int opt, Object val)
           
 
Methods inherited from class java.net.DatagramSocketImpl
getFileDescriptor, getLocalPort
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

timeout

private int timeout

trafficClass

private int trafficClass

connected

private boolean connected

connectedAddress

private InetAddress connectedAddress

connectedPort

private int connectedPort

multicastInterface

private int multicastInterface

loopbackMode

private boolean loopbackMode

ttl

private int ttl
Constructor Detail

PlainDatagramSocketImpl

PlainDatagramSocketImpl()
Method Detail

create

protected void create()
               throws SocketException
Creates a datagram socket

Specified by:
create in class DatagramSocketImpl
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.

bind

protected void bind(int lport,
                    InetAddress laddr)
             throws SocketException
Binds a datagram socket to a local port.

Specified by:
bind in class DatagramSocketImpl
Parameters:
lport - the local port
laddr - the local address
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.

send

protected void send(DatagramPacket p)
             throws IOException
Sends a datagram packet. The packet contains the data and the destination address to send the packet to.

Specified by:
send in class DatagramSocketImpl
Parameters:
p - the packet to be sent.
Throws:
IOException - if an I/O exception occurs while sending the datagram packet.

connect

protected void connect(InetAddress address,
                       int port)
                throws SocketException
Connects a datagram socket to a remote destination. This associates the remote address with the local socket so that datagrams may only be sent to this destination and received from this destination.

Overrides:
connect in class DatagramSocketImpl
Parameters:
address - the remote InetAddress to connect to
port - the remote port number
Throws:
SocketException - may be thrown if the socket cannot be connected to the remote destination

disconnect

protected void disconnect()
Disconnects a previously connected socket. Does nothing if the socket was not connected already.

Overrides:
disconnect in class DatagramSocketImpl

peek

protected int peek(InetAddress i)
            throws IOException
Peek at the packet to see who it is from.

Specified by:
peek in class DatagramSocketImpl
Parameters:
i - an InetAddress object
Returns:
the address which the packet came from.
Throws:
IOException - if an I/O exception occurs

peekData

protected int peekData(DatagramPacket p)
                throws IOException
Description copied from class: DatagramSocketImpl
Peek at the packet to see who it is from. The data is returned, but not consumed, so that a subsequent peekData/receive operation will see the same data.

Specified by:
peekData in class DatagramSocketImpl
Parameters:
p - the Packet Received.
Returns:
the address which the packet came from.
Throws:
IOException - if an I/O exception occurs

receive

protected void receive(DatagramPacket p)
                throws IOException
Receive the datagram packet.

Specified by:
receive in class DatagramSocketImpl
Parameters:
p - the Packet Received.
Throws:
IOException - if an I/O exception occurs while receiving the datagram packet.

setTimeToLive

protected void setTimeToLive(int ttl)
                      throws IOException
Set the TTL (time-to-live) option.

Specified by:
setTimeToLive in class DatagramSocketImpl
Parameters:
ttl - an int specifying the time-to-live value
Throws:
IOException - if an I/O exception occurs while setting the time-to-live option.
See Also:
DatagramSocketImpl.getTimeToLive()

getTimeToLive

protected int getTimeToLive()
                     throws IOException
Get the TTL (time-to-live) option.

Specified by:
getTimeToLive in class DatagramSocketImpl
Returns:
an int representing the time-to-live value
Throws:
IOException - if an I/O exception occurs while retrieving the time-to-live option
See Also:
DatagramSocketImpl.setTimeToLive(int)

setTTL

protected void setTTL(byte ttl)
               throws IOException
Set the TTL (time-to-live) option.

Specified by:
setTTL in class DatagramSocketImpl
Parameters:
ttl - a byte specifying the TTL value
Throws:
IOException - if an I/O exception occurs while setting the time-to-live option.
See Also:
DatagramSocketImpl.getTTL()

getTTL

protected byte getTTL()
               throws IOException
Get the TTL (time-to-live) option.

Specified by:
getTTL in class DatagramSocketImpl
Returns:
a byte representing the TTL value
Throws:
IOException - if an I/O exception occurs while retrieving the time-to-live option
See Also:
DatagramSocketImpl.setTTL(byte)

join

protected void join(InetAddress inetaddr)
             throws IOException
Join the multicast group.

Specified by:
join in class DatagramSocketImpl
Parameters:
inetaddr - multicast address to join.
Throws:
IOException - if an I/O exception occurs while joining the multicast group.

leave

protected void leave(InetAddress inetaddr)
              throws IOException
Leave the multicast group.

Specified by:
leave in class DatagramSocketImpl
Parameters:
inetaddr - multicast address to leave.
Throws:
IOException - if an I/O exception occurs while leaving the multicast group.

joinGroup

protected void joinGroup(SocketAddress mcastaddr,
                         NetworkInterface netIf)
                  throws IOException
Join the multicast group.

Specified by:
joinGroup in class DatagramSocketImpl
Parameters:
netIf - specifies the local interface to receive multicast datagram packets
mcastaddr - address to join.
Throws:
IllegalArgumentException - if mcastaddr is null or is a SocketAddress subclass not supported by this socket
IOException - if an I/O exception occurs while joining the multicast group
Since:
1.4

join

private void join(InetAddress inetaddr,
                  NetworkInterface netIf)
           throws IOException
Throws:
IOException

leaveGroup

protected void leaveGroup(SocketAddress mcastaddr,
                          NetworkInterface netIf)
                   throws IOException
Leave the multicast group.

Specified by:
leaveGroup in class DatagramSocketImpl
Parameters:
netIf - specified the local interface to leave the group at
mcastaddr - address to leave.
Throws:
IllegalArgumentException - if mcastaddr is null or is a SocketAddress subclass not supported by this socket
IOException - if an I/O exception occurs while leaving the multicast group
Since:
1.4

leave

private void leave(InetAddress inetaddr,
                   NetworkInterface netIf)
            throws IOException
Throws:
IOException

close

protected void close()
Close the socket.

Specified by:
close in class DatagramSocketImpl

finalize

protected void finalize()
Description copied from class: Object
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Overrides:
finalize in class Object

setOption

public void setOption(int optID,
                      Object o)
               throws SocketException
set a value - since we only support (setting) binary options here, o must be a Boolean

Parameters:
optID - identifies the option
o - the parameter of the socket option
Throws:
SocketException - if the option is unrecognized, the socket is closed, or some low-level error occurred
See Also:
SocketOptions.getOption(int)

getOption

public Object getOption(int optID)
                 throws SocketException
Description copied from interface: SocketOptions
Fetch the value of an option. Binary options will return java.lang.Boolean(true) if enabled, java.lang.Boolean(false) if disabled, e.g.:
 SocketImpl s;
 ...
 Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY));
 if (noDelay.booleanValue()) {
     // true if TCP_NODELAY is enabled...
 ...
 }
 

For options that take a particular type as a parameter, getOption(int) will return the paramter's value, else it will return java.lang.Boolean(false):

 Object o = s.getOption(SO_LINGER);
 if (o instanceof Integer) {
     System.out.print("Linger time is " + ((Integer)o).intValue());
 } else {
   // the true type of o is java.lang.Boolean(false);
 }
 

Parameters:
optID - an int identifying the option to fetch
Returns:
the value of the option
Throws:
SocketException - if optID is unknown along the protocol stack (including the SocketImpl)
See Also:
SocketOptions.setOption(int, java.lang.Object)

datagramSocketCreate

private void datagramSocketCreate()
                           throws SocketException
Throws:
SocketException

datagramSocketClose

private void datagramSocketClose()

socketSetOption

private void socketSetOption(int opt,
                             Object val)
                      throws SocketException
Throws:
SocketException

socketGetOption

private Object socketGetOption(int opt)
                        throws SocketException
Throws:
SocketException

connect0

private void connect0(InetAddress address,
                      int port)
               throws SocketException
Throws:
SocketException

disconnect0

private void disconnect0()

init

private static void init()
Perform class load-time initializations.