Example usage for java.nio.channels SocketChannel close

List of usage examples for java.nio.channels SocketChannel close

Introduction

In this page you can find the example usage for java.nio.channels SocketChannel close.

Prototype

public final void close() throws IOException 

Source Link

Document

Closes this channel.

Usage

From source file:org.apache.gobblin.tunnel.TestTunnelWithArbitraryTCPTraffic.java

@Test(enabled = false, timeOut = 15000)
public void testTunnelToEchoServer() throws IOException {
    MockServer proxyServer = startConnectProxyServer();
    Tunnel tunnel = Tunnel.build("localhost", doubleEchoServer.getServerSocketPort(), "localhost",
            proxyServer.getServerSocketPort());

    try {// w  w  w  .  ja v a 2 s . c  o  m
        int tunnelPort = tunnel.getPort();
        SocketChannel client = SocketChannel.open();

        client.connect(new InetSocketAddress("localhost", tunnelPort));
        client.write(ByteBuffer.wrap("Knock\n".getBytes()));
        String response = readFromSocket(client);
        client.close();

        assertEquals(response, "Knock Knock\n");
        assertEquals(proxyServer.getNumConnects(), 1);
    } finally {
        proxyServer.stopServer();
        tunnel.close();
        assertFalse(tunnel.isTunnelThreadAlive());
    }
}

From source file:org.apache.gobblin.tunnel.TestTunnelWithArbitraryTCPTraffic.java

@Test(enabled = false, timeOut = 15000)
public void testTunnelToDelayedEchoServer() throws IOException {
    MockServer proxyServer = startConnectProxyServer();
    Tunnel tunnel = Tunnel.build("localhost", delayedDoubleEchoServer.getServerSocketPort(), "localhost",
            proxyServer.getServerSocketPort());

    try {//  w  w  w . ja v  a  2  s .c om
        int tunnelPort = tunnel.getPort();
        SocketChannel client = SocketChannel.open();

        client.connect(new InetSocketAddress("localhost", tunnelPort));
        client.write(ByteBuffer.wrap("Knock\n".getBytes()));
        String response = readFromSocket(client);
        client.close();

        assertEquals(response, "Knock Knock\n");
        assertEquals(proxyServer.getNumConnects(), 1);
    } finally {
        proxyServer.stopServer();
        tunnel.close();
        assertFalse(tunnel.isTunnelThreadAlive());
    }
}

From source file:edu.hawaii.soest.pacioos.text.SocketTextSource.java

/**
* A method used to the TCP socket of the remote source host for communication
* @param host       the name or IP address of the host to connect to for the
*                   socket connection (reading)
* @param portNumber the number of the TCP port to connect to (i.e. 2604)
*///from   w ww.  j  ava 2 s.co m
protected SocketChannel getSocketConnection() {

    String host = getHostName();
    int portNumber = new Integer(getHostPort()).intValue();
    SocketChannel dataSocket = null;

    try {

        // create the socket channel connection to the data source via the 
        // converter serial2IP converter      
        dataSocket = SocketChannel.open();
        dataSocket.connect(new InetSocketAddress(host, portNumber));

        // if the connection to the source fails, also disconnect from the RBNB
        // server and return null
        if (!dataSocket.isConnected()) {
            dataSocket.close();
            disconnect();
            dataSocket = null;
        }
    } catch (UnknownHostException ukhe) {

        log.info("Unable to look up host: " + host + "\n");
        disconnect();
        dataSocket = null;
    } catch (IOException nioe) {
        log.info("Couldn't get I/O connection to: " + host + ":" + portNumber);
        disconnect();
        dataSocket = null;
    }
    return dataSocket;

}

From source file:gobblin.tunnel.TestTunnelWithArbitraryTCPTraffic.java

@Test(timeOut = 15000)
public void testDirectConnectionToEchoServer() throws IOException {
    SocketChannel client = SocketChannel.open();
    try {/* ww w.ja  va 2s  .c o  m*/
        client.connect(new InetSocketAddress("localhost", doubleEchoServer.getServerSocketPort()));
        writeToSocket(client, "Knock\n".getBytes());
        String response = readFromSocket(client);
        client.close();
        assertEquals(response, "Knock Knock\n");
    } finally {
        client.close();
    }
}

From source file:org.apache.gobblin.tunnel.TestTunnelWithArbitraryTCPTraffic.java

@Test(enabled = false, timeOut = 15000)
public void testDirectConnectionToEchoServer() throws IOException {
    SocketChannel client = SocketChannel.open();
    try {/*  ww w .j  a va2 s. c  o m*/
        client.connect(new InetSocketAddress("localhost", doubleEchoServer.getServerSocketPort()));
        writeToSocket(client, "Knock\n".getBytes());
        String response = readFromSocket(client);
        client.close();
        assertEquals(response, "Knock Knock\n");
    } finally {
        client.close();
    }
}

From source file:org.commoncrawl.io.NIOServerTCPSocket.java

public void acceptable() {
    if (_listener != null) {

        SocketChannel newClientChannel = null;
        try {//from w  w  w . j a  v a 2 s  .c om
            newClientChannel = _channel.accept();
            // set socket options
            setClientSocketOptions(newClientChannel);
            // allocate a new NIOClientTCPSocket object ...
            NIOClientTCPSocket newSocketObj = new NIOClientTCPSocket(newClientChannel);
            // inform the listener of the event
            _listener.Accepted(newSocketObj);
        } catch (Exception e) {
            LOG.error("ERROR: ACCEPTING CONNECTION On Server Socket:" + _channel.toString() + " Exception:"
                    + CCStringUtils.stringifyException(e));
            if (newClientChannel != null) {
                try {
                    newClientChannel.close();
                } catch (IOException e2) {
                    LOG.error(CCStringUtils.stringifyException(e));
                }
            }
            throw new RuntimeException(e);
        }
    }

}

From source file:Proxy.java

void close(Selector sel, SocketChannel in_channel, SocketChannel out_channel) {
    try {/*from   w  ww.ja va2  s .  c o  m*/
        if (sel != null)
            sel.close();
    } catch (Exception ex) {
    }
    try {
        if (in_channel != null)
            in_channel.close();
    } catch (Exception ex) {
    }
    try {
        if (out_channel != null)
            out_channel.close();
    } catch (Exception ex) {
    }
}

From source file:gobblin.tunnel.TestTunnelWithArbitraryTCPTraffic.java

@Test(timeOut = 15000)
public void testTunnelToEchoServerMultiRequest() throws IOException {
    MockServer proxyServer = startConnectProxyServer();
    Tunnel tunnel = Tunnel.build("localhost", doubleEchoServer.getServerSocketPort(), "localhost",
            proxyServer.getServerSocketPort());

    try {//from   w w  w . j  ava2s .c  o m
        int tunnelPort = tunnel.getPort();
        SocketChannel client = SocketChannel.open();

        client.connect(new InetSocketAddress("localhost", tunnelPort));
        client.write(ByteBuffer.wrap("Knock\n".getBytes()));
        String response1 = readFromSocket(client);

        client.write(ByteBuffer.wrap("Hello\n".getBytes()));
        String response2 = readFromSocket(client);

        client.close();

        assertEquals(response1, "Knock Knock\n");
        assertEquals(response2, "Hello Hello\n");
        assertEquals(proxyServer.getNumConnects(), 1);
    } finally {
        proxyServer.stopServer();
        tunnel.close();
        assertFalse(tunnel.isTunnelThreadAlive());
    }
}

From source file:org.apache.gobblin.tunnel.TestTunnelWithArbitraryTCPTraffic.java

@Test(enabled = false, timeOut = 15000)
public void testTunnelToEchoServerMultiRequest() throws IOException {
    MockServer proxyServer = startConnectProxyServer();
    Tunnel tunnel = Tunnel.build("localhost", doubleEchoServer.getServerSocketPort(), "localhost",
            proxyServer.getServerSocketPort());

    try {//from  www.  ja va 2 s  . co  m
        int tunnelPort = tunnel.getPort();
        SocketChannel client = SocketChannel.open();

        client.connect(new InetSocketAddress("localhost", tunnelPort));
        client.write(ByteBuffer.wrap("Knock\n".getBytes()));
        String response1 = readFromSocket(client);

        client.write(ByteBuffer.wrap("Hello\n".getBytes()));
        String response2 = readFromSocket(client);

        client.close();

        assertEquals(response1, "Knock Knock\n");
        assertEquals(response2, "Hello Hello\n");
        assertEquals(proxyServer.getNumConnects(), 1);
    } finally {
        proxyServer.stopServer();
        tunnel.close();
        assertFalse(tunnel.isTunnelThreadAlive());
    }
}

From source file:org.apache.catalina.cluster.tcp.TcpReplicationThread.java

/**
 * The actual code which drains the channel associated with
 * the given key.  This method assumes the key has been
 * modified prior to invocation to turn off selection
 * interest in OP_READ.  When this method completes it
 * re-enables OP_READ and calls wakeup() on the selector
 * so the selector will resume watching this channel.
 *//*from w w  w.j  a  v a2  s  . c om*/
private void drainChannel(SelectionKey key) throws Exception {
    boolean packetReceived = false;
    SocketChannel channel = (SocketChannel) key.channel();
    int count;
    buffer.clear(); // make buffer empty
    ObjectReader reader = (ObjectReader) key.attachment();
    // loop while data available, channel is non-blocking
    while ((count = channel.read(buffer)) > 0) {
        buffer.flip(); // make buffer readable
        int pkgcnt = reader.append(buffer.array(), 0, count);
        buffer.clear(); // make buffer empty
    }
    //check to see if any data is available
    int pkgcnt = reader.execute();
    while (pkgcnt > 0) {
        if (synchronous) {
            sendAck(key, channel);
        } //end if
        pkgcnt--;
    }
    if (count < 0) {
        // close channel on EOF, invalidates the key
        channel.close();
        return;
    }
    // resume interest in OP_READ, OP_WRITE
    key.interestOps(key.interestOps() | SelectionKey.OP_READ);
    // cycle the selector so this key is active again
    key.selector().wakeup();
}