com.ibm.net

Class SocketUtils

  • java.lang.Object
    • com.ibm.net.SocketUtils


  • public class SocketUtils
    extends java.lang.Object
    Utility class for java.net.Socket. Developers need to guard against concurrency and consistency check between the Java object and their own use of the native sockets, retrieved by the methods provided by this class. For eg. if the Java Socket/ServerSocket/SocketChannel /ServerSocketChannel is closed, previously obtained value for the native socket and any further operations with the native socket is invalid.
    • Constructor Summary

      Constructors 
      Constructor and Description
      SocketUtils() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static void configureKeepAlive(java.net.Socket s, SocketKeepAliveParameters p)
      Control behaviour of socket keepalives for the supplied Socket.
      static long[] getFileDescriptor(java.net.ServerSocket s)
      Retrieve the value of the Socket descriptor represented by the supplied ServerSocket.
      static long[] getFileDescriptor(java.nio.channels.ServerSocketChannel channel)
      Retrieve the value of the Socket descriptor represented by the supplied ServerSocketChannel.
      static long[] getFileDescriptor(java.net.Socket s)
      Retrieve the value of the Socket descriptor represented by the supplied Socket.
      static long[] getFileDescriptor(java.nio.channels.SocketChannel channel)
      Retrieve the value of the Socket descriptor represented by the supplied SocketChannel.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SocketUtils

        public SocketUtils()
    • Method Detail

      • configureKeepAlive

        public static void configureKeepAlive(java.net.Socket s,
                                              SocketKeepAliveParameters p)
                                       throws java.net.SocketException
        Control behaviour of socket keepalives for the supplied Socket. To enable or disable socket keepalives use Socket.setKeepAlive(boolean). e.g. To enable keepalive and set the keepalive idle time for this socket to 1 hour and keepalive interval to 20 seconds:
             java.net.Socket s = serverSocket.accept();
             s.setKeepAlive(true); // Enable keepalive using existing java.net.Socket API
             com.ibm.net.SocketKeepAliveParameters p = new com.ibm.net.SocketKeepAliveParameters();
             p.setIdleTime(1*60*60); // Set keepalive idle time to 1 hour
             p.setIntervalTime(20); // Set keepalive interval to 20 seconds
             com.ibm.net.SocketUtils.configureKeepAlive(s, p);
             ...
         
        Parameters:
        s - The Socket to configure.
        p - The SocketKeepAliveParameters containing the values to set.
        Throws:
        java.net.SocketException - If an error occurs while setting the keepalive parameters.
        See Also:
        SocketKeepAliveParameters, Socket.setKeepAlive(boolean)
      • getFileDescriptor

        public static long[] getFileDescriptor(java.net.ServerSocket s)
        Retrieve the value of the Socket descriptor represented by the supplied ServerSocket.
        Parameters:
        s - ServerSocket.
        Returns:
        The value of the socket represented by the supplied ServerSocket. In case of Windows (Windows XP and Windows 2003), where IPv6 is enabled, it returns an array of 2 socket descriptors, else a single element array will be returned containing the value of the native socket descriptor. It returns -1 if the socket is closed.
      • getFileDescriptor

        public static long[] getFileDescriptor(java.nio.channels.ServerSocketChannel channel)
        Retrieve the value of the Socket descriptor represented by the supplied ServerSocketChannel.
        Parameters:
        channel - ServerSocketChannel.
        Returns:
        The value of the socket represented by the supplied ServerSocketChannel. In case of Windows (Windows XP and Windows 2003), where IPv6 is enabled, it returns an array of 2 socket descriptors, else a single element array will be returned containing the value of the native socket descriptor. It returns -1 if the socket is closed.
      • getFileDescriptor

        public static long[] getFileDescriptor(java.net.Socket s)
        Retrieve the value of the Socket descriptor represented by the supplied Socket.
        Parameters:
        s - Socket.
        Returns:
        The value of the socket represented by the supplied Socket. In case of Windows (Windows XP and Windows 2003), where IPv6 is enabled, it returns an array of 2 socket descriptors, else a single element array will be returned containing the value of the native socket descriptor. It returns -1 if the socket is closed.
      • getFileDescriptor

        public static long[] getFileDescriptor(java.nio.channels.SocketChannel channel)
        Retrieve the value of the Socket descriptor represented by the supplied SocketChannel.
        Parameters:
        channel - SocketChannel.
        Returns:
        The value of the socket represented by the supplied SocketChannel. In case of Windows (Windows XP and Windows 2003), where IPv6 is enabled, it returns an array of 2 socket descriptors, else a single element array will be returned containing the value of the native socket descriptor. It returns -1 if the socket is closed.