Example usage for java.nio.channels SocketChannel socket

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

Introduction

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

Prototype

public abstract Socket socket();

Source Link

Document

Retrieves a socket associated with this channel.

Usage

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    int port = 1234; // default

    ByteBuffer buffer = ByteBuffer.wrap(GREETING.getBytes());
    ServerSocketChannel ssc = ServerSocketChannel.open();

    ssc.socket().bind(new InetSocketAddress(port));
    ssc.configureBlocking(false);//from  w w  w.ja  va 2  s  . co m

    while (true) {
        System.out.println("Waiting for connections");

        SocketChannel sc = ssc.accept();

        if (sc == null) {
            Thread.sleep(2000);
        } else {
            System.out.println("Incoming connection from: " + sc.socket().getRemoteSocketAddress());

            buffer.rewind();
            sc.write(buffer);
            sc.close();
        }
    }
}

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

private static void setClientSocketOptions(SocketChannel channel) throws IOException {
    channel.socket().setTcpNoDelay(true);
    channel.configureBlocking(false);/* w  ww .j a v  a  2 s. c  o  m*/
}

From source file:me.xingrz.prox.tcp.tunnel.RemoteTunnel.java

private static SocketChannel makeChannel() throws IOException {
    SocketChannel channel = SocketChannel.open();
    channel.configureBlocking(false);/* www.j ava 2s  .  co m*/
    channel.socket().bind(new InetSocketAddress(0));
    return channel;
}

From source file:org.commoncrawl.io.internal.NIOClientTCPSocket.java

private static void setClientSocketOptions(SocketChannel channel) throws IOException {
    channel.socket().setPerformancePreferences(0, 1, 3);
    channel.socket().setTcpNoDelay(true);
    probeAndSetSize(false, 2 << 16, 2 << 10, channel);
    probeAndSetSize(true, 2 << 15, 2 << 10, channel);
    channel.configureBlocking(false);// w  w  w.j a  va 2 s.co  m
}

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

private static void setClientSocketOptions(SocketChannel channel) throws IOException {
    channel.socket().setPerformancePreferences(0, 1, 3);
    channel.socket().setTcpNoDelay(true);
    channel.socket().setSoLinger(false, 0);
    channel.socket().setKeepAlive(true);
    probeAndSetSize(false, 2 << 16, 2 << 10, channel);
    probeAndSetSize(true, 2 << 15, 2 << 10, channel);
    channel.configureBlocking(false);/*  ww w.j  ava  2  s.co m*/
}

From source file:org.commoncrawl.io.internal.NIOClientTCPSocket.java

private static void probeAndSetSize(boolean sendSize, int targetSize, int minSize, SocketChannel channel)
        throws IOException {

    if (sendSize && channel.socket().getSendBufferSize() >= targetSize) {
        //System.out.println("SendSize is Already:" + channel.socket().getSendBufferSize());
        return;/*from w ww . ja  v  a 2 s .  c  om*/
    } else if (!sendSize && channel.socket().getReceiveBufferSize() >= targetSize) {
        //System.out.println("RcvSize is Already:" + channel.socket().getReceiveBufferSize());
        return;
    }

    do {

        int sizeOut = 0;
        if (sendSize) {
            channel.socket().setSendBufferSize(targetSize);
            sizeOut = channel.socket().getSendBufferSize();
        } else {
            channel.socket().setReceiveBufferSize(targetSize);
            sizeOut = channel.socket().getReceiveBufferSize();
        }
        if (sizeOut == targetSize)
            break;
        targetSize >>= 1;
    } while (targetSize > minSize);

}

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

private static void probeAndSetSize(boolean sendSize, int targetSize, int minSize, SocketChannel channel)
        throws IOException {

    if (sendSize && channel.socket().getSendBufferSize() >= targetSize) {
        // System.out.println("SendSize is Already:" +
        // channel.socket().getSendBufferSize());
        return;/*from ww  w .j  av a2  s .c  o  m*/
    } else if (!sendSize && channel.socket().getReceiveBufferSize() >= targetSize) {
        // System.out.println("RcvSize is Already:" +
        // channel.socket().getReceiveBufferSize());
        return;
    }

    do {

        int sizeOut = 0;
        if (sendSize) {
            channel.socket().setSendBufferSize(targetSize);
            sizeOut = channel.socket().getSendBufferSize();
        } else {
            channel.socket().setReceiveBufferSize(targetSize);
            sizeOut = channel.socket().getReceiveBufferSize();
        }
        if (sizeOut == targetSize)
            break;
        targetSize >>= 1;
    } while (targetSize > minSize);

}

From source file:xbird.util.net.PoolableSocketChannelFactory.java

private static SocketChannel createSocketChannel(final SocketAddress sockAddr, final boolean blocking,
        final int rcvbufSize) {
    final SocketChannel ch;
    try {/*from   ww  w  .ja v  a  2 s .c  o m*/
        ch = SocketChannel.open();
        ch.configureBlocking(blocking);
    } catch (IOException e) {
        LOG.error("Failed to open SocketChannel.", e);
        throw new IllegalStateException(e);
    }
    final Socket sock = ch.socket();
    if (rcvbufSize != -1) {
        try {
            sock.setReceiveBufferSize(rcvbufSize);
        } catch (SocketException e) {
            LOG.error("Failed to setReceiveBufferSize.", e);
            throw new IllegalStateException(e);
        }
    }
    final boolean connected;
    try {
        connected = ch.connect(sockAddr);
    } catch (IOException e) {
        LOG.error("Failed to connect socket: " + sockAddr, e);
        throw new IllegalStateException(e);
    }
    if (!connected) {
        throw new IllegalStateException("Failed to connect: " + sockAddr);
    }
    return ch;
}

From source file:gridool.util.xfer.TransferUtils.java

public static void send(@Nonnull final FastByteArrayOutputStream data, @Nonnull final String fileName,
        @Nullable final String writeDirPath, @Nonnull final InetAddress dstAddr, final int dstPort,
        final boolean append, final boolean sync, @Nullable final TransferClientHandler handler)
        throws IOException {
    final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort);
    SocketChannel channel = null;
    Socket socket = null;/*w w w .j  a v a2  s  . c o  m*/
    final OutputStream out;
    try {
        channel = SocketChannel.open();
        socket = channel.socket();
        socket.connect(dstSockAddr);
        out = socket.getOutputStream();
    } catch (IOException e) {
        LOG.error("failed to connect: " + dstSockAddr, e);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
        throw e;
    }

    DataInputStream din = null;
    if (sync) {
        InputStream in = socket.getInputStream();
        din = new DataInputStream(in);
    }
    final DataOutputStream dos = new DataOutputStream(out);
    final StopWatch sw = new StopWatch();
    try {
        IOUtils.writeString(fileName, dos);
        IOUtils.writeString(writeDirPath, dos);
        long nbytes = data.size();
        dos.writeLong(nbytes);
        dos.writeBoolean(append);
        dos.writeBoolean(sync);
        if (handler == null) {
            dos.writeBoolean(false);
        } else {
            dos.writeBoolean(true);
            handler.writeAdditionalHeader(dos);
        }

        // send file using zero-copy send
        data.writeTo(out);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent a file data '" + fileName + "' of " + nbytes + " bytes to " + dstSockAddr.toString()
                    + " in " + sw.toString());
        }

        if (sync) {// receive ack in sync mode
            long remoteRecieved = din.readLong();
            if (remoteRecieved != nbytes) {
                throw new IllegalStateException(
                        "Sent " + nbytes + " bytes, but remote node received " + remoteRecieved + " bytes");
            }
        }
    } catch (FileNotFoundException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } catch (IOException e) {
        LOG.error(PrintUtils.prettyPrintStackTrace(e, -1));
        throw e;
    } finally {
        IOUtils.closeQuietly(din, dos);
        IOUtils.closeQuietly(channel);
        NetUtils.closeQuietly(socket);
    }
}

From source file:gridool.util.net.PoolableSocketChannelFactory.java

private static SocketChannel createSocketChannel(final SocketAddress sockAddr, final boolean blocking,
        final int rcvbufSize) {
    final SocketChannel ch;
    try {//from   w ww.ja v a  2  s  .  co  m
        ch = SocketChannel.open();
        ch.configureBlocking(blocking);
    } catch (IOException e) {
        LOG.error("Failed to open SocketChannel.", e);
        throw new IllegalStateException(e);
    }
    final Socket sock = ch.socket();
    if (rcvbufSize != -1) {
        try {
            sock.setReceiveBufferSize(rcvbufSize);
        } catch (SocketException e) {
            LOG.error("Failed to setReceiveBufferSize.", e);
            throw new IllegalStateException(e);
        }
    }
    try {
        ch.connect(sockAddr);
    } catch (IOException e) {
        LOG.error("Failed to connect socket: " + sockAddr, e);
        throw new IllegalStateException(e);
    }
    return ch;
}