use of java.rmi.ConnectIOException in project jdk8u_jdk by JetBrains.
the class HandshakeTimeout method main.
public static void main(String[] args) throws Exception {
System.setProperty("sun.rmi.transport.tcp.handshakeTimeout", String.valueOf(TIMEOUT / 2));
/*
* Listen on port, but never process connections made to it.
*/
ServerSocket serverSocket = new ServerSocket(PORT);
/*
* Attempt RMI call to port in separate thread.
*/
Registry registry = LocateRegistry.getRegistry(PORT);
Connector connector = new Connector(registry);
Thread t = new Thread(connector);
t.setDaemon(true);
t.start();
/*
* Wait for call attempt to finished, and analyze result.
*/
t.join(TIMEOUT);
synchronized (connector) {
if (connector.success) {
throw new RuntimeException("TEST FAILED: remote call succeeded??");
}
if (connector.exception == null) {
throw new RuntimeException("TEST FAILED: remote call did not time out");
} else {
System.err.println("remote call failed with exception:");
connector.exception.printStackTrace();
System.err.println();
if (connector.exception instanceof MarshalException) {
System.err.println("TEST FAILED: MarshalException thrown, expecting " + "java.rmi.ConnectException or ConnectIOException");
} else if (connector.exception instanceof ConnectException || connector.exception instanceof ConnectIOException) {
System.err.println("TEST PASSED: java.rmi.ConnectException or " + "ConnectIOException thrown");
} else {
throw new RuntimeException("TEST FAILED: unexpected Exception thrown", connector.exception);
}
}
}
}
use of java.rmi.ConnectIOException in project jdk8u_jdk by JetBrains.
the class ConnectionAcceptor method createConnection.
/**
* Create a new connection to the remote endpoint of this channel.
* The returned connection is new. The caller must already have
* passed a security checkConnect or equivalent.
*/
private Connection createConnection() throws RemoteException {
Connection conn;
TCPTransport.tcpLog.log(Log.BRIEF, "create connection");
if (!usingMultiplexer) {
Socket sock = ep.newSocket();
conn = new TCPConnection(this, sock);
try {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
writeTransportHeader(out);
// choose protocol (single op if not reusable socket)
if (!conn.isReusable()) {
out.writeByte(TransportConstants.SingleOpProtocol);
} else {
out.writeByte(TransportConstants.StreamProtocol);
out.flush();
/*
* Set socket read timeout to configured value for JRMP
* connection handshake; this also serves to guard against
* non-JRMP servers that do not respond (see 4322806).
*/
int originalSoTimeout = 0;
try {
originalSoTimeout = sock.getSoTimeout();
sock.setSoTimeout(handshakeTimeout);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
DataInputStream in = new DataInputStream(conn.getInputStream());
byte ack = in.readByte();
if (ack != TransportConstants.ProtocolAck) {
throw new ConnectIOException(ack == TransportConstants.ProtocolNack ? "JRMP StreamProtocol not supported by server" : "non-JRMP server at remote endpoint");
}
String suggestedHost = in.readUTF();
int suggestedPort = in.readInt();
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE, "server suggested " + suggestedHost + ":" + suggestedPort);
}
// set local host name, if unknown
TCPEndpoint.setLocalHost(suggestedHost);
// do NOT set the default port, because we don't
// know if we can't listen YET...
// write out default endpoint to match protocol
// (but it serves no purpose)
TCPEndpoint localEp = TCPEndpoint.getLocalEndpoint(0, null, null);
out.writeUTF(localEp.getHost());
out.writeInt(localEp.getPort());
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE, "using " + localEp.getHost() + ":" + localEp.getPort());
}
/*
* After JRMP handshake, set socket read timeout to value
* configured for the rest of the lifetime of the
* connection. NOTE: this timeout, if configured to a
* finite duration, places an upper bound on the time
* that a remote method call is permitted to execute.
*/
try {
/*
* If socket factory had set a non-zero timeout on its
* own, then restore it instead of using the property-
* configured value.
*/
sock.setSoTimeout((originalSoTimeout != 0 ? originalSoTimeout : responseTimeout));
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
out.flush();
}
} catch (IOException e) {
if (e instanceof RemoteException)
throw (RemoteException) e;
else
throw new ConnectIOException("error during JRMP connection establishment", e);
}
} else {
try {
conn = multiplexer.openConnection();
} catch (IOException e) {
synchronized (this) {
usingMultiplexer = false;
multiplexer = null;
}
throw new ConnectIOException("error opening virtual connection " + "over multiplexed connection", e);
}
}
return conn;
}
use of java.rmi.ConnectIOException in project jdk8u_jdk by JetBrains.
the class TCPEndpoint method newSocket.
/**
* Open and return new client socket connection to endpoint.
*/
Socket newSocket() throws RemoteException {
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE, "opening socket to " + this);
}
Socket socket;
try {
RMIClientSocketFactory clientFactory = csf;
if (clientFactory == null) {
clientFactory = chooseFactory();
}
socket = clientFactory.createSocket(host, port);
} catch (java.net.UnknownHostException e) {
throw new java.rmi.UnknownHostException("Unknown host: " + host, e);
} catch (java.net.ConnectException e) {
throw new java.rmi.ConnectException("Connection refused to host: " + host, e);
} catch (IOException e) {
// We might have simply run out of file descriptors
try {
TCPEndpoint.shedConnectionCaches();
// REMIND: should we retry createSocket?
} catch (OutOfMemoryError | Exception mem) {
// don't quit if out of memory
// or shed fails non-catastrophically
}
throw new ConnectIOException("Exception creating connection to: " + host, e);
}
// TBD: should this be left up to socket factory instead?
try {
socket.setTcpNoDelay(true);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
// fix 4187495: explicitly set SO_KEEPALIVE to prevent client hangs
try {
socket.setKeepAlive(true);
} catch (Exception e) {
// ignore and proceed
}
return socket;
}
use of java.rmi.ConnectIOException in project jdk8u_jdk by JetBrains.
the class ConnectionAcceptor method writeTransportHeader.
/**
* Send transport header over stream.
*/
private void writeTransportHeader(DataOutputStream out) throws RemoteException {
try {
// write out transport header
DataOutputStream dataOut = new DataOutputStream(out);
dataOut.writeInt(TransportConstants.Magic);
dataOut.writeShort(TransportConstants.Version);
} catch (IOException e) {
throw new ConnectIOException("error writing JRMP transport header", e);
}
}
use of java.rmi.ConnectIOException in project jdk8u_jdk by JetBrains.
the class HandshakeFailure method main.
public static void main(String[] args) throws Exception {
/*
* Listen on port...
*/
ServerSocket serverSocket = new ServerSocket(PORT);
/*
* (Attempt RMI call to port in separate thread.)
*/
Registry registry = LocateRegistry.getRegistry(PORT);
Connector connector = new Connector(registry);
Thread t = new Thread(connector);
t.setDaemon(true);
t.start();
/*
* ...accept one connection from port and send non-JRMP data.
*/
Socket socket = serverSocket.accept();
socket.getOutputStream().write("Wrong way".getBytes());
socket.close();
/*
* Wait for call attempt to finish, and analyze result.
*/
t.join(TIMEOUT);
synchronized (connector) {
if (connector.success) {
throw new RuntimeException("TEST FAILED: remote call succeeded??");
}
if (connector.exception == null) {
throw new RuntimeException("TEST FAILED: remote call did not time out");
} else {
System.err.println("remote call failed with exception:");
connector.exception.printStackTrace();
System.err.println();
if (connector.exception instanceof MarshalException) {
System.err.println("TEST FAILED: MarshalException thrown, expecting " + "java.rmi.ConnectException or ConnectIOException");
} else if (connector.exception instanceof ConnectException || connector.exception instanceof ConnectIOException) {
System.err.println("TEST PASSED: java.rmi.ConnectException or " + "ConnectIOException thrown");
} else {
throw new RuntimeException("TEST FAILED: unexpected Exception thrown", connector.exception);
}
}
}
}
Aggregations