Socket setTcpNoDelay - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Socket setTcpNoDelay method.

This code example shows how to use the following methods:close, getChannel, getInetAddress, getInputStream, getLocalAddress
import org.apache.hadoop.io.IOUtils; 
import org.apache.hadoop.ipc.Server; 
import org.apache.hadoop.security.token.Token; 
 
@InterfaceAudience.Private 
public class TcpPeerServer implements PeerServer { 
  static final Log LOG = LogFactory.getLog(TcpPeerServer.class);
 
  private final ServerSocket serverSocket;
 
  public static Peer peerFromSocket(Socket socket)
      throws IOException {
    Peer peer = null;
    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 

7
CodeRank
This code example shows how to use the following methods:getInputStream, getOutputStream, setTcpNoDelay
 
    public TcpConnector connect() { 
        if (isClosed()) { 
            log.infof("Connect socket <-> %s:%d", host, port);
            try { 
                socket = new Socket(InetAddress.getByName(host), port);
                socket.setTcpNoDelay(true);
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                writer = new OutputStreamWriter(socket.getOutputStream());
            } 
            catch (UnknownHostException e) {
                log.warnf("Unknown host '%s:%d'", host, port);
                throw Lang.wrapThrow(e);
            } 
            catch (IOException e) {
                log.warnf("IOError '%s:%d'", host, port);
                throw Lang.wrapThrow(e);
            } 
        } 
        return this;
7
CodeRank
This code example shows how to use the following methods:setSoLinger, setSoTimeout, setTcpNoDelay
 
  /** 
   * Initializes the socket object 
   */ 
  private void initSocket() { 
    socket = new Socket();
    try { 
      socket.setSoLinger(false, 0);
      socket.setTcpNoDelay(true);
      socket.setSoTimeout(timeout);
    } catch (SocketException sx) {
      LOGGER.error("Could not configure socket.", sx);
    } 
  } 
 
  /** 
   * Sets the socket timeout 
   * 
   * @param timeout Milliseconds timeout 
   */ 
9
CodeRank
This code example shows how to use the following methods:getInputStream, getOutputStream, getTcpNoDelay, setTcpNoDelay
		socket.connect(pSocketAddress, pTimeoutMilliseconds);
		return new SocketConnection(socket);
	} 
 
	public SocketConnection(final Socket pSocket) throws IOException {
		super(new DataInputStream(pSocket.getInputStream()), new DataOutputStream(pSocket.getOutputStream()));
 
//		pSocket.setPerformancePreferences(1, 3, 2); 
// 
//		final int sendBufferSize = pSocket.getSendBufferSize(); 
//		pSocket.setSendBufferSize(sendBufferSize); 
// 
//		final int receiveBufferSize = pSocket.getReceiveBufferSize(); 
//		pSocket.setReceiveBufferSize(receiveBufferSize); 
 
		try { 
			if (!pSocket.getTcpNoDelay()) {
				pSocket.setTcpNoDelay(true);
			} 
		} catch (SocketException e) {

6
CodeRank
This code example shows how to use the following methods:setSoLinger, setSoTimeout, setTcpNoDelay
 
  /** 
   * Initializes the socket object 
   */ 
  private void initSocket() { 
    socket_ = new Socket();
    try { 
      socket_.setSoLinger(false, 0);
      socket_.setTcpNoDelay(true);
      socket_.setSoTimeout(timeout_);
    } catch (SocketException sx) {
      Log.e("Protocol", "Could not configure socket.", sx);
    } 
  } 
 
  /** 
   * Sets the socket timeout 
   * 
   * @param timeout Milliseconds timeout 
   */ 
6
CodeRank
This code example shows how to use the following methods:setSoLinger, setSoTimeout, setTcpNoDelay
     */ 
    protected void prepareSocket( 
            final Socket sock,
            final HttpContext context,
            final HttpParams params) throws IOException {
        sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
        sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
 
        int linger = HttpConnectionParams.getLinger(params);
        if (linger >= 0) {
            sock.setSoLinger(linger > 0, linger);
        } 
    } 
 
    /** 
     * Resolves the given host name to an array of corresponding IP addresses, based on the 
     * configured name service on the system. 
     * 
     * @param host host name to resolve 
     * @return array of IP addresses 
8
CodeRank
This code example shows how to use the following methods:setTcpNoDelay
    } 
 
    @Override 
    public SocketChannelConfig setTcpNoDelay(boolean tcpNoDelay) {
        try { 
            javaSocket.setTcpNoDelay(tcpNoDelay);
        } catch (SocketException e) {
            throw new ChannelException(e);
        } 
        return this;
    } 
 
    @Override 
    public SocketChannelConfig setTrafficClass(int trafficClass) {
        try { 
            javaSocket.setTrafficClass(trafficClass);
        } catch (SocketException e) {
            throw new ChannelException(e);
        } 
        return this;
6
CodeRank
This code example shows how to use the following methods:connect, setKeepAlive, setReuseAddress, setSoLinger, setTcpNoDelay
  } 
 
  public static Socket newSocket(String host, int port, int timeout)
    throws IOException 
  { 
    final Socket socket = new Socket();
 
    socket.setReuseAddress(true);
    socket.setKeepAlive(true);  //Will monitor the TCP connection is valid
    socket.setTcpNoDelay(true);  //Socket buffer Whetherclosed, to ensure timely delivery of data
    socket.setSoLinger(true, 0);  //Control calls close () method, the underlying socket is closed immediately
 
    socket.connect(new InetSocketAddress(host, port), timeout);
    //socket.setSoTimeout(timeout); 
 
    return socket;
  } 
} 
9
CodeRank
This code example shows how to use the following methods:close, getChannel, getInetAddress, getInputStream, getLocalAddress
@InterfaceAudience.Private 
public class TcpPeerServer implements PeerServer { 
  static Log LOG = LogFactory.getLog(TcpPeerServer.class);
 
  private final ServerSocket serverSocket;
 
  public static Peer peerFromSocket(Socket socket)
      throws IOException { 
    Peer peer = null;
    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 

9
CodeRank
This code example shows how to use the following methods:setKeepAlive, setSoTimeout, setTcpNoDelay
        throws XmppException 
    { 
        SocketFactory socketFactory = SocketFactory.getDefault();
        try { 
            socket = socketFactory.createSocket(addresse, port);
            socket.setKeepAlive(false);
            socket.setSoTimeout(3*60*1000);
            socket.setTcpNoDelay(true);
        } catch (IOException e) {
            close(); 
            throw new XmppTransportException("Can't connect", e);
        } 
        FeatureNegotiationEngine engine;
        try { 
            engine = new FeatureNegotiationEngine(socket);
        } catch (XmlPullParserException e) {
            close(); 
            throw new XmppMalformedException("Can't connect", e);
        } catch (IOException e) {
            close(); 
6
CodeRank
setTcpNoDelay Method Overview
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
See Code Examples for other Java 8 Socket Methods: