Search in sources :

Example 1 with ConnectIOException

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);
            }
        }
    }
}
Also used : MarshalException(java.rmi.MarshalException) ConnectIOException(java.rmi.ConnectIOException) ServerSocket(java.net.ServerSocket) LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) ConnectException(java.rmi.ConnectException)

Example 2 with ConnectIOException

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;
}
Also used : DataOutputStream(java.io.DataOutputStream) ConnectIOException(java.rmi.ConnectIOException) Connection(sun.rmi.transport.Connection) ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Endpoint(sun.rmi.transport.Endpoint) ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException) Socket(java.net.Socket)

Example 3 with ConnectIOException

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;
}
Also used : ConnectIOException(java.rmi.ConnectIOException) ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory)

Example 4 with ConnectIOException

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);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ConnectIOException(java.rmi.ConnectIOException) ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException)

Example 5 with ConnectIOException

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);
            }
        }
    }
}
Also used : MarshalException(java.rmi.MarshalException) ConnectIOException(java.rmi.ConnectIOException) ServerSocket(java.net.ServerSocket) LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ConnectException(java.rmi.ConnectException)

Aggregations

ConnectIOException (java.rmi.ConnectIOException)5 IOException (java.io.IOException)3 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 DataOutputStream (java.io.DataOutputStream)2 ConnectException (java.rmi.ConnectException)2 MarshalException (java.rmi.MarshalException)2 RemoteException (java.rmi.RemoteException)2 LocateRegistry (java.rmi.registry.LocateRegistry)2 Registry (java.rmi.registry.Registry)2 DataInputStream (java.io.DataInputStream)1 RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)1 Connection (sun.rmi.transport.Connection)1 Endpoint (sun.rmi.transport.Endpoint)1