Example usage for java.net Socket getReceiveBufferSize

List of usage examples for java.net Socket getReceiveBufferSize

Introduction

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

Prototype

public synchronized int getReceiveBufferSize() throws SocketException 

Source Link

Document

Gets the value of the SocketOptions#SO_RCVBUF SO_RCVBUF option for this Socket , that is the buffer size used by the platform for input on this Socket .

Usage

From source file:Main.java

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

    System.out.println(client.getReceiveBufferSize());

    client.close();/*  ww  w .j av a2 s.c  o  m*/
}

From source file:org.vietspider.net.apache.SocketInputBuffer.java

/**
 * Creates an instance of this class. /*from w  w w.j a  v  a2 s .  c  o  m*/
 * <p>
 * The following HTTP parameters affect the initialization:
 * <p>
 * The {@link CoreProtocolPNames#HTTP_ELEMENT_CHARSET}
 * parameter determines the charset to be used for decoding HTTP lines. If 
 * not specified, <code>US-ASCII</code> will be used per default.
 * <p>
 * The {@link CoreConnectionPNames#MAX_LINE_LENGTH} parameter determines 
 * the maximum line length limit. If set to a positive value, any HTTP 
 * line exceeding this limit will cause an IOException. A negative or zero 
 * value will effectively disable the check. Per default the line length 
 * check is disabled.
 *    
 * @param socket the socket to read data from. 
 * @param buffersize the size of the internal buffer. If this number is less
 *   than <code>0</code> it is set to the value of 
 *   {@link Socket#getReceiveBufferSize()}. If resultant number is less 
 *   than <code>1024</code> it is set to <code>1024</code>. 
 * @param params HTTP parameters.
 * 
 * @see CoreProtocolPNames#HTTP_ELEMENT_CHARSET
 * @see CoreConnectionPNames#MAX_LINE_LENGTH
 */
public SocketInputBuffer(final Socket socket, int buffersize, final HttpParams params) throws IOException {
    super();
    if (socket == null) {
        throw new IllegalArgumentException("Socket may not be null");
    }
    this.socket = socket;
    this.eof = false;
    if (buffersize < 0) {
        buffersize = socket.getReceiveBufferSize();
    }
    if (buffersize < 1024) {
        buffersize = 1024;
    }
    init(socket.getInputStream(), buffersize, params);
}

From source file:org.apache.sshd.SshServer.java

protected void configure(IoAcceptor acceptor) {
    if (acceptor instanceof NioSocketAcceptor) {
        final NioSocketAcceptor nio = (NioSocketAcceptor) acceptor;
        nio.setReuseAddress(reuseAddress);
        nio.setBacklog(backlog);/*from w  w  w .j  av a2 s .co  m*/

        // MINA itself forces our socket receive buffer to 1024 bytes
        // by default, despite what the operating system defaults to.
        // This limits us to about 3 MB/s incoming data transfer.  By
        // forcing back to the operating system default we can get a
        // decent transfer rate again.
        //
        final Socket s = new Socket();
        try {
            try {
                nio.getSessionConfig().setReceiveBufferSize(s.getReceiveBufferSize());
            } finally {
                s.close();
            }
        } catch (IOException e) {
            log.warn("cannot adjust SO_RCVBUF back to system default", e);
        }
    }
    if (sessionConfig != null) {
        acceptor.getSessionConfig().setAll(sessionConfig);
    }
}

From source file:org.mule.transport.http.HttpServerConnection.java

public HttpServerConnection(final Socket socket, String encoding, HttpConnector connector) throws IOException {
    super();//from  w w  w  .  j  a  v  a 2s .c om

    if (socket == null) {
        throw new IllegalArgumentException("Socket may not be null");
    }

    this.socket = socket;

    if (this.socket instanceof SSLSocket) {
        ((SSLSocket) socket).addHandshakeCompletedListener(this);
    }

    setSocketTcpNoDelay();
    this.socket.setKeepAlive(connector.isKeepAlive());

    if (connector.getReceiveBufferSize() != Connector.INT_VALUE_NOT_SET
            && socket.getReceiveBufferSize() != connector.getReceiveBufferSize()) {
        socket.setReceiveBufferSize(connector.getReceiveBufferSize());
    }
    if (connector.getServerSoTimeout() != Connector.INT_VALUE_NOT_SET
            && socket.getSoTimeout() != connector.getServerSoTimeout()) {
        socket.setSoTimeout(connector.getServerSoTimeout());
    }

    this.in = socket.getInputStream();
    this.out = new DataOutputStream(socket.getOutputStream());
    this.encoding = encoding;
}

From source file:org.mule.transport.tcp.TcpConnector.java

public void configureSocket(boolean client, Socket socket) throws SocketException {
    // There is some overhead in setting socket timeout and buffer size, so we're
    // careful here only to set if needed

    if (newValue(getReceiveBufferSize(), socket.getReceiveBufferSize())) {
        socket.setReceiveBufferSize(getReceiveBufferSize());
    }//from   ww w  . j  av  a 2  s  .  c  om
    if (newValue(getSendBufferSize(), socket.getSendBufferSize())) {
        socket.setSendBufferSize(getSendBufferSize());
    }
    if (client) {
        if (newValue(getClientSoTimeout(), socket.getSoTimeout())) {
            socket.setSoTimeout(getClientSoTimeout());
        }
    } else {
        if (newValue(getServerSoTimeout(), socket.getSoTimeout())) {
            socket.setSoTimeout(getServerSoTimeout());
        }
    }
    if (newValue(getSocketSoLinger(), socket.getSoLinger())) {
        socket.setSoLinger(true, getSocketSoLinger());
    }
    try {
        socket.setTcpNoDelay(isSendTcpNoDelay());
    } catch (SocketException e) {
        // MULE-2800 - Bug in Solaris
    }
    socket.setKeepAlive(isKeepAlive());
}

From source file:org.springframework.integration.ip.tcp.connection.TcpNioConnectionTests.java

@Test
public void testInsufficientThreads() throws Exception {
    final ExecutorService exec = Executors.newFixedThreadPool(2);
    Future<Object> future = exec.submit(() -> {
        SocketChannel channel = mock(SocketChannel.class);
        Socket socket = mock(Socket.class);
        Mockito.when(channel.socket()).thenReturn(socket);
        doAnswer(invocation -> {/*from w w w  .j  a v a2  s .c o m*/
            ByteBuffer buffer = invocation.getArgument(0);
            buffer.position(1);
            return 1;
        }).when(channel).read(Mockito.any(ByteBuffer.class));
        when(socket.getReceiveBufferSize()).thenReturn(1024);
        final TcpNioConnection connection = new TcpNioConnection(channel, false, false, nullPublisher, null);
        connection.setTaskExecutor(exec);
        connection.setPipeTimeout(200);
        Method method = TcpNioConnection.class.getDeclaredMethod("doRead");
        method.setAccessible(true);
        // Nobody reading, should timeout on 6th write.
        try {
            for (int i = 0; i < 6; i++) {
                method.invoke(connection);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw (Exception) e.getCause();
        }
        return null;
    });
    try {
        Object o = future.get(10, TimeUnit.SECONDS);
        fail("Expected exception, got " + o);
    } catch (ExecutionException e) {
        assertEquals("Timed out waiting for buffer space", e.getCause().getMessage());
    }
}

From source file:voldemort.store.socket.SocketPoolableObjectFactory.java

private void recordSocketCreation(SocketDestination dest, Socket socket) throws SocketException {
    int numCreated = created.incrementAndGet();
    logger.debug("Created socket " + numCreated + " for " + dest.getHost() + ":" + dest.getPort());

    // check buffer sizes--you often don't get out what you put in!
    int sendBufferSize = socket.getSendBufferSize();
    int receiveBufferSize = socket.getReceiveBufferSize();
    if (receiveBufferSize != this.socketBufferSize)
        logger.debug("Requested socket receive buffer size was " + this.socketBufferSize
                + " bytes but actual size is " + receiveBufferSize + " bytes.");
    if (sendBufferSize != this.socketBufferSize)
        logger.debug("Requested socket send buffer size was " + this.socketBufferSize
                + " bytes but actual size is " + sendBufferSize + " bytes.");
}