List of usage examples for java.net Socket setTcpNoDelay
public void setTcpNoDelay(boolean on) throws SocketException
From source file:com.flipkart.phantom.thrift.impl.proxy.SocketObjectFactory.java
/** * Interface method implementation. Checks if the socket is open and then attempts to set Thrift specific socket properties. * An error in any of these operations will invalidate the specified Socket. * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(Object) */// ww w . j av a2 s .c om public boolean validateObject(Socket socket) { if (socket.isClosed()) { return false; } try { socket.setSoLinger(false, 0); socket.setTcpNoDelay(true); return true; } catch (Exception e) { LOGGER.info("Socket is not valid for server : {} at port : {}", this.getThriftProxy().getThriftServer(), this.getThriftProxy().getThriftPort()); return false; } }
From source file:voldemort.store.socket.SocketPoolableObjectFactory.java
/** * Create a socket for the given host/port *//*ww w . j a v a 2 s . co m*/ public Object makeObject(Object key) throws Exception { SocketDestination dest = (SocketDestination) key; Socket socket = new Socket(); socket.setReceiveBufferSize(this.socketBufferSize); socket.setSendBufferSize(this.socketBufferSize); socket.setTcpNoDelay(true); socket.setSoTimeout(soTimeoutMs); socket.connect(new InetSocketAddress(dest.getHost(), dest.getPort())); recordSocketCreation(dest, socket); return new SocketAndStreams(socket); }
From source file:com.qiniu.android.http.ClientConnectionOperator.java
/** * Performs standard initializations on a newly created socket. * * @param sock the socket to prepare//from w w w .j a v a2 s. c o m * @param context the context for the connection * @param params the parameters from which to prepare the socket * @throws IOException in case of an IO problem */ protected void prepareSocket(final Socket sock, final HttpContext context, final HttpParams params) throws IOException { sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params)); sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params)); final int linger = HttpConnectionParams.getLinger(params); if (linger >= 0) { sock.setSoLinger(linger > 0, linger); } }
From source file:org.parosproxy.paros.core.proxy.WebSocketsConnectionIntegrationTest.java
@Test public void doAutobahnTest() throws HttpException, SocketException { // use HTTP-client with custom connection manager // that allows us to expose the SocketChannel HttpClient client = new HttpClient(new ZapHttpConnectionManager()); client.getHostConfiguration().setProxy(PROXY_HOST, PROXY_PORT); // create minimal HTTP handshake request ZapGetMethod method = new ZapGetMethod("http://localhost:9001/runCase?case=1&agent=Proxy"); method.addRequestHeader("Connection", "upgrade"); method.addRequestHeader("Upgrade", "websocket"); method.addRequestHeader("Sec-WebSocket-Version", "13"); method.addRequestHeader("Sec-WebSocket-Key", "5d5NazNjJ5hafSgFYJ7SOw=="); try {//from ww w.java 2 s .c o m client.executeMethod(method); } catch (IOException e) { assertTrue("executing HTTP method failed", false); } assertEquals("HTTP status code of WebSockets-handshake response should be 101.", 101, method.getStatusCode()); Socket socket = method.getUpgradedConnection(); socket.setTcpNoDelay(true); socket.setSoTimeout(500); byte[] dst = new byte[20]; try { socket.getInputStream().read(dst); } catch (IOException e) { assertTrue("reading websocket frame failed: " + e.getMessage(), false); } }
From source file:org.apache.http2.impl.conn.DefaultClientConnectionOperator.java
/** * Performs standard initializations on a newly created socket. * * @param sock the socket to prepare * @param context the context for the connection * @param params the parameters from which to prepare the socket * * @throws IOException in case of an IO problem *//* ww w . j ava 2 s. co m*/ 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); } }
From source file:com.newrelic.agent.deps.org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.java
@Override public void connect(final ManagedHttpClientConnection conn, final HttpHost host, final InetSocketAddress localAddress, final int connectTimeout, final SocketConfig socketConfig, final HttpContext context) throws IOException { final Lookup<ConnectionSocketFactory> registry = getSocketFactoryRegistry(context); final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName()); if (sf == null) { throw new UnsupportedSchemeException(host.getSchemeName() + " protocol is not supported"); }//from w w w .ja v a 2 s . co m final InetAddress[] addresses = host.getAddress() != null ? new InetAddress[] { host.getAddress() } : this.dnsResolver.resolve(host.getHostName()); final int port = this.schemePortResolver.resolve(host); for (int i = 0; i < addresses.length; i++) { final InetAddress address = addresses[i]; final boolean last = i == addresses.length - 1; Socket sock = sf.createSocket(context); sock.setSoTimeout(socketConfig.getSoTimeout()); sock.setReuseAddress(socketConfig.isSoReuseAddress()); sock.setTcpNoDelay(socketConfig.isTcpNoDelay()); sock.setKeepAlive(socketConfig.isSoKeepAlive()); final int linger = socketConfig.getSoLinger(); if (linger >= 0) { sock.setSoLinger(true, linger); } conn.bind(sock); final InetSocketAddress remoteAddress = new InetSocketAddress(address, port); if (this.log.isDebugEnabled()) { this.log.debug("Connecting to " + remoteAddress); } try { sock = sf.connectSocket(connectTimeout, sock, host, remoteAddress, localAddress, context); conn.bind(sock); if (this.log.isDebugEnabled()) { this.log.debug("Connection established " + conn); } return; } catch (final SocketTimeoutException ex) { if (last) { throw new ConnectTimeoutException(ex, host, addresses); } } catch (final ConnectException ex) { if (last) { final String msg = ex.getMessage(); if ("Connection timed out".equals(msg)) { throw new ConnectTimeoutException(ex, host, addresses); } else { throw new HttpHostConnectException(ex, host, addresses); } } } catch (final NoRouteToHostException ex) { if (last) { throw ex; } } if (this.log.isDebugEnabled()) { this.log.debug("Connect to " + remoteAddress + " timed out. " + "Connection will be retried using another IP address"); } } }
From source file:com.subgraph.vega.internal.http.proxy.VegaHttpServerConnection.java
public void bind(final Socket socket, final HttpParams params) throws IOException { if (socket == null) { throw new IllegalArgumentException("Socket may not be null"); }//from w w w .jav a2s. c om if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } socket.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params)); socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params)); int linger = HttpConnectionParams.getLinger(params); if (linger >= 0) { socket.setSoLinger(linger > 0, linger); } super.bind(socket, params); }
From source file:net.jradius.server.TCPListener.java
/** * Listen for one object and place it on the request queue * @throws IOException//from w ww . ja v a 2 s . c om * @throws InterruptedException * @throws RadiusException */ public void listen() throws Exception { RadiusLog.debug("Listening on socket..."); Socket socket = serverSocket.accept(); socket.setTcpNoDelay(false); if (keepAlive) { KeepAliveListener keepAliveListener = new KeepAliveListener(socket, this, queue); keepAliveListener.start(); synchronized (keepAliveListeners) { keepAliveListeners.add(keepAliveListener); } } else { TCPListenerRequest lr = (TCPListenerRequest) requestObjectPool.borrowObject(); lr.setBorrowedFromPool(requestObjectPool); lr.accept(socket, this, false, false); while (true) { try { this.queue.put(lr); break; } catch (InterruptedException e) { } } } }
From source file:org.parosproxy.paros.core.proxy.WebSocketsConnectionIntegrationTest.java
/** * Sends a Hello message into the channel and asserts that * the same message is returned by the Echo-Server. * The outgoing message is masked, while the incoming * contains the message in cleartext./*from ww w .j a va2 s. c om*/ * * @param socket * @throws IOException */ private void assertWorkingWebSocket(Socket socket) throws IOException { assertTrue("Retrieved SocketChannel should not be null.", socket != null); socket.setSoTimeout(500); socket.setTcpNoDelay(true); socket.setKeepAlive(true); byte[] maskedHelloMessage = { (byte) 0x81, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 }; byte[] unmaskedHelloMessage = { (byte) 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f }; InputStream inpStream = new BufferedInputStream(socket.getInputStream()); OutputStream out = socket.getOutputStream(); out.write(maskedHelloMessage); out.flush(); byte[] dst = new byte[7]; inpStream.read(dst); // use Arrays class to compare two byte arrays // returns true if it contains the same elements in same order assertTrue("Awaited unmasked hello message from echo server.", Arrays.equals(unmaskedHelloMessage, dst)); }
From source file:hudson.remoting.Engine.java
/** * Connects to TCP slave port, with a few retries. *//*w ww .j av a 2 s . com*/ private Socket connect(String port) throws IOException, InterruptedException { String host = this.hudsonUrl.getHost(); if (tunnel != null) { String[] tokens = tunnel.split(":", 3); if (tokens.length != 2) throw new IOException("Illegal tunneling parameter: " + tunnel); if (tokens[0].length() > 0) host = tokens[0]; if (tokens[1].length() > 0) port = tokens[1]; } String msg = "Connecting to " + host + ':' + port; listener.status(msg); int retry = 1; while (true) { try { Socket s = new Socket(host, Integer.parseInt(port)); s.setTcpNoDelay(true); // we'll do buffering by ourselves // set read time out to avoid infinite hang. the time out should be long enough so as not // to interfere with normal operation. the main purpose of this is that when the other peer dies // abruptly, we shouldn't hang forever, and at some point we should notice that the connection // is gone. s.setSoTimeout(30 * 60 * 1000); // 30 mins. See PingThread for the ping interval return s; } catch (IOException e) { if (retry++ > 10) throw (IOException) new IOException("Failed to connect to " + host + ':' + port).initCause(e); Thread.sleep(1000 * 10); listener.status(msg + " (retrying:" + retry + ")", e); } } }