Package org.h2.util
Class NetUtils
java.lang.Object
org.h2.util.NetUtils
This utility class contains socket helper functions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ServerSocketcloseSilently(ServerSocket socket) Close a server socket and ignore any exceptions.static SocketcreateLoopbackSocket(int port, boolean ssl) Create a loopback socket (a socket that is connected to localhost) on this port.static ServerSocketcreateServerSocket(int port, boolean ssl) Create a server socket.static SocketcreateSocket(String server, int defaultPort, boolean ssl) Create a client socket that is connected to the given address and port.static SocketcreateSocket(String server, int defaultPort, boolean ssl, int networkTimeout) Create a client socket that is connected to the given address and port.static SocketcreateSocket(InetAddress address, int port, boolean ssl) Create a client socket that is connected to the given address and port.static SocketcreateSocket(InetAddress address, int port, boolean ssl, int networkTimeout) Create a client socket that is connected to the given address and port.static StringgetHostName(String localAddress) Get the host name of a local address, if available.static StringGet the local host address as a string.static StringBuilderipToShortForm(StringBuilder builder, byte[] address, boolean addBrackets) Appends short representation of the specified IP address to the string builder.static booleanisLocalAddress(Socket socket) Check if a socket is connected to a local address.
-
Method Details
-
createLoopbackSocket
Create a loopback socket (a socket that is connected to localhost) on this port.- Parameters:
port- the portssl- if SSL should be used- Returns:
- the socket
- Throws:
IOException- on failure
-
createSocket
Create a client socket that is connected to the given address and port.- Parameters:
server- to connect to (including an optional port)defaultPort- the default port (if not specified in the server address)ssl- if SSL should be used- Returns:
- the socket
- Throws:
IOException- on failure
-
createSocket
public static Socket createSocket(String server, int defaultPort, boolean ssl, int networkTimeout) throws IOException Create a client socket that is connected to the given address and port.- Parameters:
server- to connect to (including an optional port)defaultPort- the default port (if not specified in the server address)ssl- if SSL should be usednetworkTimeout- socket so timeout- Returns:
- the socket
- Throws:
IOException- on failure
-
createSocket
Create a client socket that is connected to the given address and port.- Parameters:
address- the address to connect toport- the portssl- if SSL should be used- Returns:
- the socket
- Throws:
IOException- on failure
-
createSocket
public static Socket createSocket(InetAddress address, int port, boolean ssl, int networkTimeout) throws IOException Create a client socket that is connected to the given address and port.- Parameters:
address- the address to connect toport- the portssl- if SSL should be usednetworkTimeout- socket so timeout- Returns:
- the socket
- Throws:
IOException- on failure
-
createServerSocket
Create a server socket. The system property h2.bindAddress is used if set.- Parameters:
port- the port to listen onssl- if SSL should be used- Returns:
- the server socket
-
isLocalAddress
Check if a socket is connected to a local address.- Parameters:
socket- the socket- Returns:
- true if it is
- Throws:
UnknownHostException- on failure
-
closeSilently
Close a server socket and ignore any exceptions.- Parameters:
socket- the socket- Returns:
- null
-
getLocalAddress
Get the local host address as a string. For performance, the result is cached for one second.- Returns:
- the local host address
-
getHostName
Get the host name of a local address, if available.- Parameters:
localAddress- the local address- Returns:
- the host name, or another text if not available
-
ipToShortForm
public static StringBuilder ipToShortForm(StringBuilder builder, byte[] address, boolean addBrackets) Appends short representation of the specified IP address to the string builder.- Parameters:
builder- string builder to append to, ornulladdress- IP addressaddBrackets- if (true, add brackets around IPv6 addresses- Returns:
- the specified or the new string builder with short representation of specified address
-