Example usage for java.net Socket setTcpNoDelay

List of usage examples for java.net Socket setTcpNoDelay

Introduction

In this page you can find the example usage for java.net Socket setTcpNoDelay.

Prototype

public void setTcpNoDelay(boolean on) throws SocketException 

Source Link

Document

Enable/disable SocketOptions#TCP_NODELAY TCP_NODELAY (disable/enable Nagle's algorithm).

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket client = new Socket("google.com", 80);
    client.setTcpNoDelay(true);
    System.out.println(client.getTcpNoDelay());

    client.close();//from  w w  w.jav  a2  s .c o m
}

From source file:net.oneandone.sushi.fs.webdav.WebdavConnection.java

public static WebdavConnection open(Socket socket, HttpParams params) throws IOException {
    int linger;/* w w  w .ja va2  s . co  m*/
    int buffersize;
    SessionInputBuffer input;
    SessionOutputBuffer output;

    socket.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
    socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
    linger = HttpConnectionParams.getLinger(params);
    if (linger >= 0) {
        socket.setSoLinger(linger > 0, linger);
    }
    buffersize = HttpConnectionParams.getSocketBufferSize(params);
    if (WebdavFilesystem.WIRE.isLoggable(Level.FINE)) {
        input = new LoggingSessionInputBuffer(socket, buffersize, params, WebdavFilesystem.WIRE);
        output = new LoggingSessionOutputBuffer(socket, buffersize, params, WebdavFilesystem.WIRE);
    } else {
        input = new SocketInputBuffer(socket, buffersize, params);
        output = new SocketOutputBuffer(socket, buffersize, params);
    }
    return new WebdavConnection(socket, input, output, params);
}

From source file:com.machinepublishers.jbrowserdriver.StreamConnectionClient.java

private static Socket newSocket(final HttpContext context) throws IOException {
    InetSocketAddress proxySocks = (InetSocketAddress) context.getAttribute("proxy.socks.address");
    Socket socket;
    if (proxySocks != null) {
        socket = new Socket(new Proxy(Proxy.Type.SOCKS, proxySocks));
    } else {/*from   w  w w .  ja v a  2s. c o  m*/
        socket = new Socket();
    }
    socket.setTcpNoDelay(true);
    socket.setKeepAlive(true);
    return socket;
}

From source file:org.apache.hadoop.hdfs.net.TcpPeerServer.java

public static Peer peerFromSocket(Socket socket) throws IOException {
    Peer peer = null;// w  ww .  j a  v a2  s .com
    boolean success = false;
    try {
        // TCP_NODELAY is crucial here because of bad interactions between
        // Nagle's Algorithm and Delayed ACKs. With connection keepalive
        // between the client and DN, the conversation looks like:
        //   1. Client -> DN: Read block X
        //   2. DN -> Client: data for block X
        //   3. Client -> DN: Status OK (successful read)
        //   4. Client -> DN: Read block Y
        // The fact that step #3 and #4 are both in the client->DN direction
        // triggers Nagling. If the DN is using delayed ACKs, this results
        // in a delay of 40ms or more.
        //
        // TCP_NODELAY disables nagling and thus avoids this performance
        // disaster.
        socket.setTcpNoDelay(true);
        SocketChannel channel = socket.getChannel();
        if (channel == null) {
            peer = new BasicInetPeer(socket);
        } else {
            peer = new NioInetPeer(socket);
        }
        success = true;
        return peer;
    } finally {
        if (!success) {
            if (peer != null)
                peer.close();
            socket.close();
        }
    }
}

From source file:com.smartwork.client.gsocket.CommonSocketFactory.java

public Object makeObject(Object key) throws Exception {
    ServerAddress address = (ServerAddress) key;
    Socket conn = new Socket();
    conn.setSoTimeout(networkConfig.getReadTimeout() * 1000);
    conn.setTcpNoDelay(networkConfig.isTcpNoDelay());
    conn.setReuseAddress(networkConfig.isReuseAddress());
    conn.setSoLinger(networkConfig.getSoLinger() > 0, networkConfig.getSoLinger());
    conn.setSendBufferSize(networkConfig.getSendBufferSize());
    conn.setReceiveBufferSize(networkConfig.getReceiveBufferSize());
    conn.connect(new InetSocketAddress(address.getHost(), address.getPort()),
            networkConfig.getConnectTimeout() * 1000);
    return conn;/*  w w  w.  j a v  a  2  s . co m*/
}

From source file:org.squidy.manager.commander.ControlServer.java

@Override
public void run() {
    try {//  www. jav  a2 s.  c o m
        incomings = new ArrayList<Incoming>();
        outgoings = new ArrayList<Outgoing>();

        while (running) {
            Socket client = server.accept();
            client.setTcpNoDelay(true);

            if (LOG.isInfoEnabled()) {
                LOG.info("Client connected: " + client.getInetAddress() + " on port " + client.getPort());
            }

            clients.add(client);

            Incoming incoming = new Incoming(client.getInputStream(), context);
            Outgoing outgoing = new Outgoing(client.getOutputStream(), context);

            ConnectionPeer connectionPeer = new ConnectionPeer(incoming, outgoing);

            incomings.add(incoming);
            outgoings.add(outgoing);
        }

        for (Incoming incoming : incomings) {
            incoming.close();
        }

        for (Outgoing outgoing : outgoings) {
            outgoing.close();
        }

        for (Socket client : clients) {
            client.close();
        }
    } catch (IOException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error occured in control server: " + e.getMessage(), e);
        }
    }
}

From source file:org.hashes.HttpClient.java

protected void applySettings(final Socket socket) throws SocketException {
    socket.setTcpNoDelay(true);
    socket.setSoTimeout(this.target.getReadTimeout());
}

From source file:com.tasktop.c2c.server.web.proxy.ajp.AjpPoolableConnectionFactory.java

@Override
public Object makeObject(Object objectKey) throws Exception {
    Key key = (Key) objectKey;
    String host = key.getHost();// www . ja  v a  2s .co m
    int port = key.getPort();
    if (port <= 0) {
        port = 8009;
    }

    Socket socket = socketFactory.createSocket(host, port);
    try {
        socket.setTcpNoDelay(tcpNoDelay);
        socket.setSoTimeout(soTimeout);
        socket.setKeepAlive(keepAlive);
    } catch (SocketException e) {
        socket.close();
        throw e;
    }
    debug("Created new socket: " + socket.toString());
    return socket;
}

From source file:org.apache.hadoop.hdfs.server.datanode.DataXceiverServer.java

/**
 *//* w  ww  .  j  a  v a 2  s  . co m*/
public void run() {
    while (datanode.shouldRun) {
        try {
            Socket s = ss.accept();
            s.setTcpNoDelay(true);
            new Daemon(datanode.threadGroup, new DataXceiver(s, datanode, this)).start();
        } catch (SocketTimeoutException ignored) {
            // wake up to see if should continue to run
        } catch (AsynchronousCloseException ace) {
            LOG.warn(datanode.dnRegistration + ":DataXceiveServer:" + StringUtils.stringifyException(ace));
            datanode.shouldRun = false;
        } catch (IOException ie) {
            LOG.warn(datanode.dnRegistration + ":DataXceiveServer: IOException due to:"
                    + StringUtils.stringifyException(ie));
        } catch (Throwable te) {
            LOG.error(datanode.dnRegistration + ":DataXceiveServer: Exiting due to:"
                    + StringUtils.stringifyException(te));
            datanode.shouldRun = false;
        }
    }
    try {
        ss.close();
    } catch (IOException ie) {
        LOG.warn(datanode.dnRegistration + ":DataXceiveServer: Close exception due to: "
                + StringUtils.stringifyException(ie));
    }
    LOG.info("Exiting DataXceiveServer");
}

From source file:org.rhq.plugins.platform.content.yum.YumServer.java

/**
 * Listen for and process requests./*from ww w  . j  a  v  a 2s.c o m*/
 */
private void listen() {
    try {
        Socket client = socket.accept();
        client.setTcpNoDelay(true);
        client.setSoLinger(false, 0);
        Request request = new Request(this, client);
        request.process();
    } catch (SocketTimeoutException te) {
        // expected
    } catch (Exception e) {
        log.warn("listen failed", e);
        run = false;
    }
}