com.act365.net
Class GeneralDatagramSocketImpl

java.lang.Object
  |
  +--java.net.DatagramSocketImpl
        |
        +--com.act365.net.GeneralDatagramSocketImpl
All Implemented Interfaces:
java.net.SocketOptions
Direct Known Subclasses:
ICMPDatagramSocketImpl, RawICMPDatagramSocketImpl, RawTCPDatagramSocketImpl, RawTCPJDatagramSocketImpl, RawUDPDatagramSocketImpl, TCPDatagramSocketImpl, TCPJDatagramSocketImpl, UDPDatagramSocketImpl

public abstract class GeneralDatagramSocketImpl
extends java.net.DatagramSocketImpl

GeneralDatagramSocketImpl extends java.net.DatagramSocketImpl and provides native implementations of all of its abstract methods. The class remains abstract because it leaves its create() method undefined - it is up to subclasses to specify the parameters that will be used to create the underlying socket.


Fields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, SO_BINDADDR, SO_KEEPALIVE, SO_LINGER, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
GeneralDatagramSocketImpl()
           
 
Method Summary
 void bind(int localPort, java.net.InetAddress inetAddress)
          Binds this socket to a local port.
 void close()
          Closes this socket.
 void create(int socketType, int protocol, boolean headerincluded)
          Creates a new unconnected socket.
 java.lang.Object getOption(int optID)
          Gets the value of a socket option.
static int getSocketDescriptor(java.io.FileDescriptor fd)
          Gets the socket descriptor from a java.io.FileDescriptor object.
 int getTimeToLive()
          Retrieves time-to-live for multicast sockets.
 byte getTTL()
          Retrieves time-to-live for multicast sockets.
 void join(java.net.InetAddress groupAddr)
          Joins a multicast group.
 void joinGroup(java.net.SocketAddress mcastaddr, java.net.NetworkInterface netIf)
          Joins a multicast group.
 void leave(java.net.InetAddress groupAddr)
          Leaves a multicast group.
 void leaveGroup(java.net.SocketAddress mcastaddr, java.net.NetworkInterface netIf)
          Leaves a multicast group.
 int peek(java.net.InetAddress sender)
          Polls until a datagram packet is received.
 int peekData(java.net.DatagramPacket p)
          Extracts the next DatagramPacket and returns the port number.
 void receive(java.net.DatagramPacket dgram)
          Reads a datagram packet.
 void send(java.net.DatagramPacket dgram)
          Sends a datagram packet.
 void setOption(int optID, java.lang.Object value)
          Sets the value of a socket option.
static void setSocketDescriptor(java.io.FileDescriptor fd, int sd)
          Sets the socket descriptor for a java.io.FileDescriptor object.
 void setTimeToLive(int ttl)
          Sets time-to-live for multicast sockets.
 void setTTL(byte ttl)
          Sets time-to-live for multicast sockets.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GeneralDatagramSocketImpl

public GeneralDatagramSocketImpl()
Method Detail

create

public void create(int socketType,
                   int protocol,
                   boolean headerincluded)
            throws java.net.SocketException
Creates a new unconnected socket. The socket is allowed to be of any type and use any protocol supported by the underlying operating system. The method create() - note no arguments - should be defined in a subclass and call the version in the superclass with the parameters set appropriately. When a raw socket is set up, the user has the option to write his own IP header sockets (set headerincluded to true) or to allow the operating system to write the headers. NB The option does not exist on Windows, where the IP_HDRINCL symbol is unsupported.
Parameters:
socketType - the socket type as defined in SocketConstants e.g. SOCK_RAW
protocol - the protocol as defined in SocketConstants e.g. IPPROTO_UDP
headerincluded - whether the user will complete the IP header
See Also:
SocketConstants

bind

public void bind(int localPort,
                 java.net.InetAddress inetAddress)
          throws java.net.SocketException
Binds this socket to a local port.
Parameters:
localPort - number of the local port
inetAddress - local IP address

close

public void close()
Closes this socket.

peek

public int peek(java.net.InetAddress sender)
         throws java.io.IOException
Polls until a datagram packet is received. Once received, the parameter sender is populated with the address of the sender and the port number is returned as an int. Note that the JDK1.4 API documentation states that the IP address should be returned as an int - however, the JDK1.1 API documentation states that the port number should be returned. I have implemented the JDK1.1 behaviour because it seems more sensible - an IP address is always stored in an InetAddress object in Java.
Parameters:
sender - address of sender - to be populated by the function call
Returns:
port used by sender

peekData

public int peekData(java.net.DatagramPacket p)
             throws java.io.IOException
Extracts the next DatagramPacket and returns the port number. Note that the behaviour differs from that described in the JDK1.4 documentation.
Parameters:
p - next packet received
Returns:
port to have received the packet
See Also:
peek

receive

public void receive(java.net.DatagramPacket dgram)
             throws java.io.IOException
Reads a datagram packet.
Parameters:
dgram - packet to be populated

send

public void send(java.net.DatagramPacket dgram)
          throws java.io.IOException
Sends a datagram packet.
Parameters:
dgram - packet to be sent

getTimeToLive

public int getTimeToLive()
                  throws java.io.IOException
Retrieves time-to-live for multicast sockets. NB Multicast sockets are not yet supported.

getTTL

public byte getTTL()
            throws java.io.IOException
Retrieves time-to-live for multicast sockets. NB Multicast sockets are not yet supported. The method leads to a deprecation warning in JDK1.3 but has to be provided if java.net.DatagramSocketImpl is to be extended.

join

public void join(java.net.InetAddress groupAddr)
          throws java.io.IOException
Joins a multicast group. (JDK1.3) NB Multicast sockets are not yet supported.

joinGroup

public void joinGroup(java.net.SocketAddress mcastaddr,
                      java.net.NetworkInterface netIf)
               throws java.io.IOException
Joins a multicast group. (JDK1.4) NB Multicast sockets are not yet supported.

leave

public void leave(java.net.InetAddress groupAddr)
           throws java.io.IOException
Leaves a multicast group. (JDK1.3) NB Multicast sockets are not yet supported.

leaveGroup

public void leaveGroup(java.net.SocketAddress mcastaddr,
                       java.net.NetworkInterface netIf)
                throws java.io.IOException
Leaves a multicast group. (JDK1.4) NB Multicast sockets are not yet supported.

setTimeToLive

public void setTimeToLive(int ttl)
                   throws java.io.IOException
Sets time-to-live for multicast sockets. NB Multicast sockets are not yet supported.

setTTL

public void setTTL(byte ttl)
            throws java.io.IOException
Sets time-to-live for multicast sockets. NB Multicast sockets are not yet supported. The method leads to a deprecation warning in JDK1.3 but has to be provided if java.net.DatagramSocketImpl is to be extended.

getSocketDescriptor

public static int getSocketDescriptor(java.io.FileDescriptor fd)
Gets the socket descriptor from a java.io.FileDescriptor object. NB Java provides no public access to the value of the descriptor so it has to be extracted using native code.

setSocketDescriptor

public static void setSocketDescriptor(java.io.FileDescriptor fd,
                                       int sd)
Sets the socket descriptor for a java.io.FileDescriptor object. NB Java provides no public access to the descriptor so its value has to be set using native code.

setOption

public void setOption(int optID,
                      java.lang.Object value)
               throws java.net.SocketException
Sets the value of a socket option. The value has to be an Integer object.
Overrides:
setOption in class java.net.DatagramSocketImpl
Parameters:
optID - option as defined in java.net.SocketConstants
value - an Integer object that wraps the new value

getOption

public java.lang.Object getOption(int optID)
                           throws java.net.SocketException
Gets the value of a socket option. The returned value will be an Integer object.
Overrides:
getOption in class java.net.DatagramSocketImpl
Parameters:
optID - option as defined in java.net.SocketConstants