Search in sources :

Example 56 with ConnectException

use of java.net.ConnectException in project flink by apache.

the class KvStateClientTest method testRequestUnavailableHost.

/**
	 * Tests that a request to an unavailable host is failed with ConnectException.
	 */
@Test
public void testRequestUnavailableHost() throws Exception {
    Deadline deadline = TEST_TIMEOUT.fromNow();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateClient client = null;
    try {
        client = new KvStateClient(1, stats);
        int availablePort = NetUtils.getAvailablePort();
        KvStateServerAddress serverAddress = new KvStateServerAddress(InetAddress.getLocalHost(), availablePort);
        Future<byte[]> future = client.getKvState(serverAddress, new KvStateID(), new byte[0]);
        try {
            Await.result(future, deadline.timeLeft());
            fail("Did not throw expected ConnectException");
        } catch (ConnectException ignored) {
        // Expected
        }
    } finally {
        if (client != null) {
            client.shutDown();
        }
        assertEquals("Channel leak", 0, stats.getNumConnections());
    }
}
Also used : Deadline(scala.concurrent.duration.Deadline) KvStateServerAddress(org.apache.flink.runtime.query.KvStateServerAddress) KvStateID(org.apache.flink.runtime.query.KvStateID) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 57 with ConnectException

use of java.net.ConnectException in project screenbird by adamhub.

the class ClientIPC method connect.

/**
     * Connects to SocketServer using the SOCKET_NAME and SOCK_PORT
     * @return True if client is successful in connecting, false otherwise
     */
public boolean connect() {
    try {
        log("Connecting to Base Server...");
        this.client = new Socket(ServerIPC.SOCKET_NAME, ServerIPC.SOCKET_PORT);
        log("Connected to Base Server");
        log("Client Initiated...");
        return true;
    } catch (ConnectException e) {
        log(e);
    } catch (UnknownHostException e) {
        log("Could not connect to " + ServerIPC.SOCKET_NAME + ":" + ServerIPC.SOCKET_PORT);
    } catch (IOException e) {
        log("Could not connect to " + ServerIPC.SOCKET_NAME + ":" + ServerIPC.SOCKET_PORT);
        log(e);
    } catch (NullPointerException e) {
        log(e);
    }
    return false;
}
Also used : UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 58 with ConnectException

use of java.net.ConnectException in project screenbird by adamhub.

the class Client method connectToBaseServer.

private boolean connectToBaseServer() {
    try {
        System.out.println("Connecting to Base Server...");
        this.client = new Socket(SOCKET_NAME, SOCKET_PORT);
        ipcManager.setIsRunning(true);
        System.out.println("Connected to Base Server");
        System.out.println("Client Initiated...");
    } catch (ConnectException e) {
        e.printStackTrace(System.err);
        System.exit(-1);
    } catch (UnknownHostException e) {
        System.err.println("Could not connect to " + SOCKET_NAME + ":" + SOCKET_PORT);
        e.printStackTrace(System.err);
    } catch (IOException e) {
        System.err.println("Could not connect to " + SOCKET_NAME + ":" + SOCKET_PORT);
        e.printStackTrace(System.err);
    } catch (NullPointerException e) {
        e.printStackTrace(System.err);
    }
    return false;
}
Also used : UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 59 with ConnectException

use of java.net.ConnectException in project sonarqube by SonarSource.

the class EmbeddedTomcatTest method start.

@Test
public void start() throws Exception {
    Props props = new Props(new Properties());
    // prepare file system
    File home = temp.newFolder();
    File data = temp.newFolder();
    File webDir = new File(home, "web");
    FileUtils.write(new File(home, "web/WEB-INF/web.xml"), "<web-app/>");
    props.set("sonar.path.home", home.getAbsolutePath());
    props.set("sonar.path.data", data.getAbsolutePath());
    props.set("sonar.path.web", webDir.getAbsolutePath());
    props.set("sonar.path.logs", temp.newFolder().getAbsolutePath());
    // start server on a random port
    int httpPort = NetworkUtils.freePort();
    int ajpPort = NetworkUtils.freePort();
    props.set("sonar.web.port", String.valueOf(httpPort));
    props.set("sonar.ajp.port", String.valueOf(ajpPort));
    EmbeddedTomcat tomcat = new EmbeddedTomcat(props);
    assertThat(tomcat.getStatus()).isEqualTo(EmbeddedTomcat.Status.DOWN);
    tomcat.start();
    assertThat(tomcat.getStatus()).isEqualTo(EmbeddedTomcat.Status.UP);
    // check that http connector accepts requests
    URL url = new URL("http://" + Inet4Address.getLocalHost().getHostAddress() + ":" + httpPort);
    url.openConnection().connect();
    // stop server
    tomcat.terminate();
    // tomcat.isUp() must not be called. It is used to wait for server startup, not shutdown.
    try {
        url.openConnection().connect();
        fail();
    } catch (ConnectException e) {
    // expected
    }
}
Also used : Props(org.sonar.process.Props) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 60 with ConnectException

use of java.net.ConnectException in project openhab1-addons by openhab.

the class EBusTCPConnector method connect.

/*
     * (non-Javadoc)
     * 
     * @see org.openhab.binding.ebus.connection.AbstractEBusConnector#connect()
     */
@Override
protected boolean connect() throws IOException {
    try {
        socket = new Socket(hostname, port);
        socket.setSoTimeout(20000);
        socket.setKeepAlive(true);
        socket.setTcpNoDelay(true);
        socket.setTrafficClass((byte) 0x10);
        // Useful? We try it
        // socket.setReceiveBufferSize(1);
        socket.setSendBufferSize(1);
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
        return super.connect();
    } catch (ConnectException e) {
        logger.error(e.toString());
    } catch (Exception e) {
        logger.error(e.toString(), e);
    }
    return false;
}
Also used : Socket(java.net.Socket) IOException(java.io.IOException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Aggregations

ConnectException (java.net.ConnectException)253 IOException (java.io.IOException)110 Socket (java.net.Socket)57 Test (org.junit.Test)43 InetSocketAddress (java.net.InetSocketAddress)41 SocketTimeoutException (java.net.SocketTimeoutException)35 UnknownHostException (java.net.UnknownHostException)30 InetAddress (java.net.InetAddress)24 FileNotFoundException (java.io.FileNotFoundException)22 InterruptedIOException (java.io.InterruptedIOException)20 SocketException (java.net.SocketException)20 NoRouteToHostException (java.net.NoRouteToHostException)19 InputStreamReader (java.io.InputStreamReader)18 URL (java.net.URL)16 InputStream (java.io.InputStream)14 HttpURLConnection (java.net.HttpURLConnection)14 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)13 BufferedReader (java.io.BufferedReader)12 OutputStream (java.io.OutputStream)12 Exchange (org.apache.camel.Exchange)12