com.ibm.net
Class SocketUtils
- java.lang.Object
-
- com.ibm.net.SocketUtils
-
public class SocketUtils extends java.lang.Object
Utility class forjava.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 suppliedSocket
.static long[]
getFileDescriptor(java.net.ServerSocket s)
Retrieve the value of the Socket descriptor represented by the suppliedServerSocket
.static long[]
getFileDescriptor(java.nio.channels.ServerSocketChannel channel)
Retrieve the value of the Socket descriptor represented by the suppliedServerSocketChannel
.static long[]
getFileDescriptor(java.net.Socket s)
Retrieve the value of the Socket descriptor represented by the suppliedSocket
.static long[]
getFileDescriptor(java.nio.channels.SocketChannel channel)
Retrieve the value of the Socket descriptor represented by the suppliedSocketChannel
.
-
-
-
Method Detail
-
configureKeepAlive
public static void configureKeepAlive(java.net.Socket s, SocketKeepAliveParameters p) throws java.net.SocketException
Control behaviour of socket keepalives for the suppliedSocket
. To enable or disable socket keepalives useSocket.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
- TheSocket
to configure.p
- TheSocketKeepAliveParameters
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 suppliedServerSocket
.- 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 suppliedServerSocketChannel
.- 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 suppliedSocket
.- 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 suppliedSocketChannel
.- 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.
-
-