RMISocketFactory instance is used by the RMI runtime
in order to obtain client and server sockets for RMI calls. An
application may use the setSocketFactory method to
request that the RMI runtime use its socket factory instance
instead of the default implementation.
The default socket factory implementation creates a direct socket connection to the remote host.
The default socket factory implementation creates server sockets that are bound to the wildcard address, which accepts requests from all network interfaces.
Implementation Note
You can use the RMISocketFactory class to create a server socket that
is bound to a specific address, restricting the origin of requests. For example,
the following code implements a socket factory that binds server sockets to an IPv4
loopback address. This restricts RMI to processing requests only from the local host.
class LoopbackSocketFactory extends RMISocketFactory {
public ServerSocket createServerSocket(int port) throws IOException {
return new ServerSocket(port, 5, InetAddress.getByName("127.0.0.1"));
}
public Socket createSocket(String host, int port) throws IOException {
// just call the default client socket factory
return RMISocketFactory.getDefaultSocketFactory()
.createSocket(host, port);
}
}
// ...
RMISocketFactory.setSocketFactory(new LoopbackSocketFactory());
Set the java.rmi.server.hostname system property
to 127.0.0.1 to ensure that the generated stubs connect to the right
network interface.
| Modifier and Type | Field and Description |
|---|---|
| private static RMISocketFactory | defaultSocketFactory
default socket factory used by this RMI implementation |
| private static RMISocketFactory | factory
Client/server socket factory to be used by RMI runtime |
| private static RMIFailureHandler | handler
Handler for socket creation failure |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public abstract ServerSocket | Returns: the server socket on the specified portthe port number port)Redeclares java. Create a server socket on the specified port (port 0 indicates an anonymous port). |
| public abstract Socket | Returns: a socket connected to the specified host and port.the host name host, int the port number port)Redeclares java. Creates a client socket connected to the specified host and port. |
| public static synchronized RMISocketFactory | Returns: the default RMI socket factoryReturns a reference to the default socket factory used by this RMI implementation. |
| public static synchronized RMIFailureHandler | Returns: the failure handlerReturns the handler for socket creation failure set by the
|
| public static synchronized RMISocketFactory | Returns: the socket factoryReturns the socket factory set by the |
| public static synchronized void | setFailureHandler(RMIFailureHandler
the failure handler. fh)Sets the failure handler to be called by the RMI runtime if server socket creation fails. |
| public static synchronized void | setSocketFactory(RMISocketFactory
the socket factory fac)Set the global socket factory from which RMI gets sockets (if the remote object is not associated with a specific client and/or server socket factory). |
| defaultSocketFactory | back to summary |
|---|---|
| private static RMISocketFactory defaultSocketFactory default socket factory used by this RMI implementation | |
| factory | back to summary |
|---|---|
| private static RMISocketFactory factory Client/server socket factory to be used by RMI runtime | |
| handler | back to summary |
|---|---|
| private static RMIFailureHandler handler Handler for socket creation failure | |
| RMISocketFactory | back to summary |
|---|---|
| public RMISocketFactory() Constructs an
| |
| createServerSocket | back to summary |
|---|---|
| public abstract ServerSocket createServerSocket(int port) throws IOException Redeclares java. Create a server socket on the specified port (port 0 indicates an anonymous port).
| |
| createSocket | back to summary |
|---|---|
| public abstract Socket createSocket(String host, int port) throws IOException Redeclares java. Creates a client socket connected to the specified host and port.
| |
| getDefaultSocketFactory | back to summary |
|---|---|
| public static synchronized RMISocketFactory getDefaultSocketFactory() Returns a reference to the default socket factory used
by this RMI implementation. This will be the factory used
by the RMI runtime when
| |
| getFailureHandler | back to summary |
|---|---|
| public static synchronized RMIFailureHandler getFailureHandler() Returns the handler for socket creation failure set by the
| |
| getSocketFactory | back to summary |
|---|---|
| public static synchronized RMISocketFactory getSocketFactory() Returns the socket factory set by the
| |
| setFailureHandler | back to summary |
|---|---|
| public static synchronized void setFailureHandler(RMIFailureHandler fh) Sets the failure handler to be called by the RMI runtime if server socket creation fails. By default, if no failure handler is installed and server socket creation fails, the RMI runtime does attempt to recreate the server socket.
| |
| setSocketFactory | back to summary |
|---|---|
| public static synchronized void setSocketFactory(RMISocketFactory fac) throws IOException Set the global socket factory from which RMI gets sockets (if the remote object is not associated with a specific client and/or server socket factory). The RMI socket factory can only be set once.
| |