java.net.Socket.setReceiveBufferSize()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(263)

本文整理了Java中java.net.Socket.setReceiveBufferSize()方法的一些代码示例,展示了Socket.setReceiveBufferSize()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Socket.setReceiveBufferSize()方法的具体详情如下:
包路径:java.net.Socket
类名称:Socket
方法名:setReceiveBufferSize

Socket.setReceiveBufferSize介绍

[英]Sets this socket's SocketOptions#SO_RCVBUF.
[中]设置此套接字的SocketOptions#SO _RCVBUF。

代码示例

代码示例来源:origin: apache/kafka

private void configureSocketChannel(SocketChannel socketChannel, int sendBufferSize, int receiveBufferSize)
    throws IOException {
  socketChannel.configureBlocking(false);
  Socket socket = socketChannel.socket();
  socket.setKeepAlive(true);
  if (sendBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE)
    socket.setSendBufferSize(sendBufferSize);
  if (receiveBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE)
    socket.setReceiveBufferSize(receiveBufferSize);
  socket.setTcpNoDelay(true);
}

代码示例来源:origin: wildfly/wildfly

static void set(Socket socket) throws SocketException {
  socket.setTcpNoDelay(true);
  socket.setReceiveBufferSize(20000000);
  socket.setSendBufferSize(10000000);
}

代码示例来源:origin: mpetazzoni/ttorrent

public static void setBuffersSizeIfNecessary(SocketChannel socketChannel, int sendBufferSize, int receiveBufferSize) throws IOException {
  final Socket socket = socketChannel.socket();
  if (sendBufferSize > 0) {
   socket.setSendBufferSize(sendBufferSize);
  }
  if (receiveBufferSize > 0) {
   socket.setReceiveBufferSize(receiveBufferSize);
  }
 }
}

代码示例来源:origin: whitesock/open-replicator

/**
 * 
 */
public Socket create(String host, int port) throws Exception {
  final Socket r = new Socket(host, port);
  r.setKeepAlive(this.keepAlive);
  r.setTcpNoDelay(this.tcpNoDelay);
  if(this.receiveBufferSize > 0) r.setReceiveBufferSize(this.receiveBufferSize);
  return r;
}

代码示例来源:origin: org.postgresql/postgresql

newStream.getSocket().setKeepAlive(requireTCPKeepAlive);
  newStream.getSocket().setReceiveBufferSize(receiveBufferSize);
 } else {
  LOGGER.log(Level.WARNING, "Ignore invalid value for receiveBufferSize: {0}", receiveBufferSize);
if (sendBufferSize > -1) {
 if (sendBufferSize > 0) {
  newStream.getSocket().setSendBufferSize(sendBufferSize);
 } else {
  LOGGER.log(Level.WARNING, "Ignore invalid value for sendBufferSize: {0}", sendBufferSize);

代码示例来源:origin: apache/geode

socket.setKeepAlive(ENABLE_TCP_KEEP_ALIVE);
 socket.setReceiveBufferSize(socketBufferSize);
 socket.setKeepAlive(ENABLE_TCP_KEEP_ALIVE);
  socket.setReceiveBufferSize(socketBufferSize);

代码示例来源:origin: libgdx/libgdx

private void applyHints (SocketHints hints) {
  if (hints != null) {
    try {
      socket.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
        hints.performancePrefBandwidth);
      socket.setTrafficClass(hints.trafficClass);
      socket.setTcpNoDelay(hints.tcpNoDelay);
      socket.setKeepAlive(hints.keepAlive);
      socket.setSendBufferSize(hints.sendBufferSize);
      socket.setReceiveBufferSize(hints.receiveBufferSize);
      socket.setSoLinger(hints.linger, hints.lingerDuration);
      socket.setSoTimeout(hints.socketTimeout);
    } catch (Exception e) {
      throw new GdxRuntimeException("Error setting socket hints.", e);
    }
  }
}

代码示例来源:origin: rapidoid/rapidoid

private void configureSocket(SocketChannel socketChannel) throws IOException {
  socketChannel.configureBlocking(false);
  Socket socket = socketChannel.socket();
  socket.setTcpNoDelay(noDelay);
  socket.setReceiveBufferSize(bufSize);
  socket.setSendBufferSize(bufSize);
  socket.setReuseAddress(true);
}

代码示例来源:origin: wildfly/wildfly

protected Connection getConnection(SocketAddress dest) throws Exception {
  Connection conn=connections.get(dest);
  if(conn != null)
    return conn;
  Socket dest_sock=new Socket();
  dest_sock.setSendBufferSize(send_buf_size);
  dest_sock.setReceiveBufferSize(recv_buf_size);
  dest_sock.connect(dest);
  Connection c=connections.putIfAbsent(dest, conn=new Connection(dest_sock).start());
  if(c != null) {
    Util.close(conn);
    return c;
  }
  return conn;
}

代码示例来源:origin: com.zendesk/open-replicator

/**
 *
 */
public Socket create(String host, int port) throws Exception {
  final Socket r = new Socket(host, port);
  r.setKeepAlive(this.keepAlive);
  r.setTcpNoDelay(this.tcpNoDelay);
  if(this.receiveBufferSize > 0) r.setReceiveBufferSize(this.receiveBufferSize);
  return r;
}

代码示例来源:origin: ggrandes/bouncer

public static void setupSocket(final Socket sock) throws SocketException {
  sock.setKeepAlive(true);
  sock.setReuseAddress(true);
  sock.setSoTimeout(Constants.READ_TIMEOUT);
  sock.setSendBufferSize(Math.max(sock.getSendBufferSize(), Constants.BUFFER_LEN * Constants.IO_BUFFERS));
  sock.setReceiveBufferSize(Math.max(sock.getReceiveBufferSize(), Constants.BUFFER_LEN
      * Constants.IO_BUFFERS));
}

代码示例来源:origin: stackoverflow.com

import java.net.Socket;
public class Main {
  public static void main(String[] args) {
    try {
      Socket socket = new Socket("%%%%.%%%%%.dk", PORT NUMBER);
      socket.setReceiveBufferSize(2048);
      socket.setKeepAlive(true);
      System.out.println("socket " + socket.isConnected());
    } catch (Exception e) {
      System.out.println("error " + e.getMessage());
    }
  }
}

代码示例来源:origin: apache/rocketmq

public HAConnection(final HAService haService, final SocketChannel socketChannel) throws IOException {
  this.haService = haService;
  this.socketChannel = socketChannel;
  this.clientAddr = this.socketChannel.socket().getRemoteSocketAddress().toString();
  this.socketChannel.configureBlocking(false);
  this.socketChannel.socket().setSoLinger(false, -1);
  this.socketChannel.socket().setTcpNoDelay(true);
  this.socketChannel.socket().setReceiveBufferSize(1024 * 64);
  this.socketChannel.socket().setSendBufferSize(1024 * 64);
  this.writeSocketService = new WriteSocketService(this.socketChannel);
  this.readSocketService = new ReadSocketService(this.socketChannel);
  this.haService.getConnectionCount().incrementAndGet();
}

代码示例来源:origin: rapidoid/rapidoid

private void configureSocket(SocketChannel socketChannel) throws IOException {
  socketChannel.configureBlocking(false);
  Socket socket = socketChannel.socket();
  socket.setTcpNoDelay(noDelay);
  socket.setReceiveBufferSize(bufSize);
  socket.setSendBufferSize(bufSize);
  socket.setReuseAddress(true);
}

代码示例来源:origin: wildfly/wildfly

public void init(String local_addr, String remote_addr, int local_port, int remote_port) throws Exception {
  local=new InetSocketAddress(local_addr, local_port);
  remote=new InetSocketAddress(remote_addr, remote_port);
  srv_sock=Util.createServerSocket(new DefaultSocketFactory(), "server", local.getAddress(), local.getPort(), local.getPort());
  System.out.println("Listening on " + srv_sock.getLocalSocketAddress());
  acceptor=new Acceptor();
  acceptor.start();
  sock=new Socket();
  //sock.bind(local);
  sock.setSendBufferSize(SOCK_SEND_BUF_SIZE);
  sock.setReceiveBufferSize(SOCK_RECV_BUF_SIZE);
  try {
    sock.connect(remote);
    output=new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
    System.out.println("Connected to " + sock.getRemoteSocketAddress());
  }
  catch(Throwable t) {
    System.out.println("Failed connecting to " + remote + ": will only act as server");
  }
}

代码示例来源:origin: com.orientechnologies/orientdb-enterprise

public OChannelTextServer(final Socket iSocket, final OContextConfiguration iConfiguration) throws IOException {
    super(iSocket, iConfiguration);

    socket.setKeepAlive(true);
    socket.setPerformancePreferences(1, 2, 0);
    socket.setSendBufferSize(socketBufferSize);
    socket.setReceiveBufferSize(socketBufferSize);

    inStream = new BufferedInputStream(socket.getInputStream(), socketBufferSize);
    outStream = new BufferedOutputStream(socket.getOutputStream(), socketBufferSize);
  }
}

代码示例来源:origin: libgdx/libgdx

private void applyHints (SocketHints hints) {
  if (hints != null) {
    try {
      socket.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
        hints.performancePrefBandwidth);
      socket.setTrafficClass(hints.trafficClass);
      socket.setTcpNoDelay(hints.tcpNoDelay);
      socket.setKeepAlive(hints.keepAlive);
      socket.setSendBufferSize(hints.sendBufferSize);
      socket.setReceiveBufferSize(hints.receiveBufferSize);
      socket.setSoLinger(hints.linger, hints.lingerDuration);
      socket.setSoTimeout(hints.socketTimeout);
    } catch (Exception e) {
      throw new GdxRuntimeException("Error setting socket hints.", e);
    }
  }
}

代码示例来源:origin: wildfly/wildfly

public void run() {
  while(!srv_sock.isClosed()) {
    Socket client_sock=null;
    DataInputStream in=null;
    try {
      client_sock=srv_sock.accept();
      client_sock.setTcpNoDelay(TCP_NODELAY);
      client_sock.setReceiveBufferSize(SOCK_RECV_BUF_SIZE);
      client_sock.setSendBufferSize(SOCK_SEND_BUF_SIZE);
      in=new DataInputStream(new BufferedInputStream(client_sock.getInputStream()));
      while(!client_sock.isClosed())
        handleRequest(in);
    }
    catch(Exception e) {
      Util.close(client_sock);
      Util.close(in);
    }
  }
}

代码示例来源:origin: wildfly/wildfly

public void run() {
    try {
      Socket client_sock=srv_sock.accept();
      client_sock.setSendBufferSize(send_buf_size);
      client_sock.setReceiveBufferSize(recv_buf_size);
      Connection c;
      Connection existing=connections.putIfAbsent(client_sock.getRemoteSocketAddress(),
                            c=new Connection(client_sock).start());
      if(existing != null)
        Util.close(c);
    }
    catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: apache/rocketmq

public static SocketChannel connect(SocketAddress remote, final int timeoutMillis) {
  SocketChannel sc = null;
  try {
    sc = SocketChannel.open();
    sc.configureBlocking(true);
    sc.socket().setSoLinger(false, -1);
    sc.socket().setTcpNoDelay(true);
    sc.socket().setReceiveBufferSize(1024 * 64);
    sc.socket().setSendBufferSize(1024 * 64);
    sc.socket().connect(remote, timeoutMillis);
    sc.configureBlocking(false);
    return sc;
  } catch (Exception e) {
    if (sc != null) {
      try {
        sc.close();
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    }
  }
  return null;
}

相关文章