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

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

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

ServerSocket.setReceiveBufferSize介绍

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

代码示例

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

@Override
public ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
  try {
    javaSocket.setReceiveBufferSize(receiveBufferSize);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
  return this;
}

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

@Override
public ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
  try {
    javaSocket.setReceiveBufferSize(receiveBufferSize);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
  return this;
}

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

@Override
public ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
  try {
    javaSocket.setReceiveBufferSize(receiveBufferSize);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
  return this;
}

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

public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) {
  this.protocol = protocol;
  // create the server socket
  try {
    // initialize
    server = new java.net.ServerSocket();
    if (hints != null) {
      server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
        hints.performancePrefBandwidth);
      server.setReuseAddress(hints.reuseAddress);
      server.setSoTimeout(hints.acceptTimeout);
      server.setReceiveBufferSize(hints.receiveBufferSize);
    }
    // and bind the server...
    InetSocketAddress address;
    if( hostname != null ) {
      address = new InetSocketAddress(hostname, port); 
    } else {
      address = new InetSocketAddress(port);
    }
    
    if (hints != null) {
      server.bind(address, hints.backlog);
    } else {
      server.bind(address);
    }
  } catch (Exception e) {
    throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e);
  }
}

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

public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) {
  this.protocol = protocol;
  // create the server socket
  try {
    // initialize
    server = new java.net.ServerSocket();
    if (hints != null) {
      server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency,
        hints.performancePrefBandwidth);
      server.setReuseAddress(hints.reuseAddress);
      server.setSoTimeout(hints.acceptTimeout);
      server.setReceiveBufferSize(hints.receiveBufferSize);
    }
    // and bind the server...
    InetSocketAddress address;
    if( hostname != null ) {
      address = new InetSocketAddress(hostname, port); 
    } else {
      address = new InetSocketAddress(port);
    }
    
    if (hints != null) {
      server.bind(address, hints.backlog);
    } else {
      server.bind(address);
    }
  } catch (Exception e) {
    throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e);
  }
}

代码示例来源:origin: io.netty/netty

public void setReceiveBufferSize(int receiveBufferSize) {
  try {
    socket.setReceiveBufferSize(receiveBufferSize);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
}

代码示例来源:origin: loklak/loklak_server

private static void checkServerPorts(int httpPort, int httpsPort) throws IOException{

    // check http port
    if(!httpsMode.equals(HttpsMode.ONLY)){
      try (ServerSocket ss = new ServerSocket(httpPort)) {
        ss.setReuseAddress(true);
        ss.setReceiveBufferSize(65536);
      } catch (IOException e) {
        // the socket is already occupied by another service
        throw new IOException("port " + httpPort + " is already occupied by another service, maybe another loklak is running on this port already. exit.");
      }
    }

    // check https port
    if(httpsMode.isGreaterOrEqualTo(HttpsMode.ON)){
      try (ServerSocket sss = new ServerSocket(httpsPort)) {
        sss.setReuseAddress(true);
        sss.setReceiveBufferSize(65536);
      } catch (IOException e) {
        // the socket is already occupied by another service
        throw new IOException("port " + httpsPort + " is already occupied by another service, maybe another loklak is running on this port already. exit.");
      }
    }
  }
}

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

@Override
protected void startInner() {
  if(logger.isEnabledFor(Level.INFO))
    logger.info("Starting Voldemort NIO socket server (" + serviceName + ") on port "
          + port);
  try {
    for(int i = 0; i < selectorManagers.length; i++) {
      selectorManagers[i] = new NioSelectorManager(endpoint,
                             requestHandlerFactory,
                             socketBufferSize,
                             socketKeepAlive,
                             selectorMaxHeartBeatTimeMs);
      selectorManagerThreadPool.execute(selectorManagers[i]);
    }
    serverSocketChannel.socket().bind(endpoint, acceptorBacklog);
    serverSocketChannel.socket().setReceiveBufferSize(socketBufferSize);
    serverSocketChannel.socket().setReuseAddress(true);
    acceptorThread.start();
  } catch(Exception e) {
    throw new VoldemortException(e);
  }
  enableJmx(this);
}

代码示例来源:origin: TooTallNate/Java-WebSocket

/**
 * Setup the selector thread as well as basic server settings
 * @return true, if everything was successful, false if some error happened
 */
private boolean doSetupSelectorAndServerThread() {
  selectorthread.setName( "WebSocketSelector-" + selectorthread.getId() );
  try {
    server = ServerSocketChannel.open();
    server.configureBlocking( false );
    ServerSocket socket = server.socket();
    socket.setReceiveBufferSize( WebSocketImpl.RCVBUF );
    socket.setReuseAddress( isReuseAddr() );
    socket.bind( address );
    selector = Selector.open();
    server.register( selector, server.validOps() );
    startConnectionLostTimer();
    onStart();
  } catch ( IOException ex ) {
    handleFatal( null, ex );
    return false;
  }
  return true;
}

代码示例来源:origin: jphp-group/jphp

@Signature(@Arg("size"))
public Memory setReceiveBufferSize(Environment env, Memory... args) throws SocketException {
  socket.setReceiveBufferSize(args[0].toInteger());
  return Memory.NULL;
}

代码示例来源:origin: h2oai/h2o-2

SOCK.socket().setReceiveBufferSize(AutoBuffer.BBSIZE);
SOCK.socket().bind(H2O.SELF._key);

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

public ServerSocket createServerSocket(int nport, int backlog, InetAddress bindAddr,
  List<GatewayTransportFilter> transportFilters, int socketBufferSize) throws IOException {
 if (transportFilters.isEmpty()) {
  return createServerSocket(nport, backlog, bindAddr, socketBufferSize);
 } else {
  printConfig();
  ServerSocket result = new TransportFilterServerSocket(transportFilters);
  result.setReuseAddress(true);
  // Set the receive buffer size before binding the socket so
  // that large buffers will be allocated on accepted sockets (see
  // java.net.ServerSocket.setReceiverBufferSize javadocs)
  result.setReceiveBufferSize(socketBufferSize);
  try {
   result.bind(new InetSocketAddress(bindAddr, nport), backlog);
  } catch (BindException e) {
   BindException throwMe =
     new BindException(String.format("Failed to create server socket on %s[%s]",
       bindAddr, Integer.valueOf(nport)));
   throwMe.initCause(e);
   throw throwMe;
  }
  return result;
 }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private void init() {
  ServerSocketChannel socketChannel = processor.javaChannel();
  ServerSocket javaSocket = socketChannel.socket();
  try {
    if (serverConfig.getReceiveBufferSize() != null) {
      javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize());
    }
    if (serverConfig.getReuseAddress() != null) {
      javaSocket.setReuseAddress(serverConfig.getReuseAddress());
    }
  } catch (SocketException e) {
    throw new NioException("config channel error:" + e.getMessage(), e);
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private void init() {
  ServerSocketChannel socketChannel = processor.javaChannel();
  ServerSocket javaSocket = socketChannel.socket();
  try {
    if (serverConfig.getReceiveBufferSize() != null) {
      javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize());
    }
    if (serverConfig.getReuseAddress() != null) {
      javaSocket.setReuseAddress(serverConfig.getReuseAddress());
    }
  } catch (SocketException e) {
    throw new NioException("config channel error:" + e.getMessage(), e);
  }
}

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

srvrCh.socket().setReceiveBufferSize(sockRcvBuf);

代码示例来源:origin: square/okio

final ServerSocket serverSocket = new ServerSocket(0);
serverSocket.setReuseAddress(true);
serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);

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

protected AcceptingChannel<StreamConnection> createTcpConnectionServer(final InetSocketAddress bindAddress, final ChannelListener<? super AcceptingChannel<StreamConnection>> acceptListener, final OptionMap optionMap) throws IOException {
  checkShutdown();
  boolean ok = false;
  final ServerSocketChannel channel = ServerSocketChannel.open();
  try {
    if (optionMap.contains(Options.RECEIVE_BUFFER)) channel.socket().setReceiveBufferSize(optionMap.get(Options.RECEIVE_BUFFER, -1));
    channel.socket().setReuseAddress(optionMap.get(Options.REUSE_ADDRESSES, true));
    channel.configureBlocking(false);
    if (optionMap.contains(Options.BACKLOG)) {
      channel.socket().bind(bindAddress, optionMap.get(Options.BACKLOG, 128));
    } else {
      channel.socket().bind(bindAddress);
    }
    if (false) {
      final NioTcpServer server = new NioTcpServer(this, channel, optionMap);
      server.setAcceptListener(acceptListener);
      ok = true;
      return server;
    } else {
      final QueuedNioTcpServer server = new QueuedNioTcpServer(this, channel, optionMap);
      server.setAcceptListener(acceptListener);
      ok = true;
      return server;
    }
  } finally {
    if (! ok) {
      IoUtils.safeClose(channel);
    }
  }
}

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

public static ServerSocket createServerSocket(final int port, final ServerSocketConfiguration config)
    throws IOException, KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException {
  if (config == null) {
    throw new NullPointerException("Configuration may not be null.");
  }
  final SSLContext sslContext = config.createSSLContext();
  final ServerSocket serverSocket;
  if (sslContext == null) {
    serverSocket = new ServerSocket(port);
  } else {
    serverSocket = sslContext.getServerSocketFactory().createServerSocket(port);
    ((SSLServerSocket) serverSocket).setNeedClientAuth(config.getNeedClientAuth());
  }
  if (config.getSocketTimeout() != null) {
    serverSocket.setSoTimeout(config.getSocketTimeout());
  }
  if (config.getReuseAddress() != null) {
    serverSocket.setReuseAddress(config.getReuseAddress());
  }
  if (config.getReceiveBufferSize() != null) {
    serverSocket.setReceiveBufferSize(config.getReceiveBufferSize());
  }
  return serverSocket;
}

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

public void open() throws Exception {
  serverSocket = createServerSocket(target);
  serverSocket.setReuseAddress(true);
  if (receiveBufferSize > 0) {
    serverSocket.setReceiveBufferSize(receiveBufferSize);
  }
  if (proxyUrl == null) {
    serverSocket.bind(new InetSocketAddress(listenPort), acceptBacklog);
    proxyUrl = urlFromSocket(target, serverSocket);
  } else {
    serverSocket.bind(new InetSocketAddress(proxyUrl.getPort()));
  }
  acceptor = new Acceptor(serverSocket, target);
  if (pauseAtStart) {
    acceptor.pause();
  }
  new Thread(null, acceptor, "SocketProxy-Acceptor-" + serverSocket.getLocalPort()).start();
  closed = new CountDownLatch(1);
}

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

private void openSocket() throws IOException {
  U.notNull(net.protocol(), "protocol");
  U.notNull(net.helperClass(), "helperClass");
  String blockingInfo = net.blockingAccept() ? "blocking" : "non-blocking";
  Log.debug("Initializing server", "address", net.address(), "port", net.port(), "sync", net.syncBufs(), "accept", blockingInfo);
  serverSocketChannel = ServerSocketChannel.open();
  if ((serverSocketChannel.isOpen()) && (selector.isOpen())) {
    serverSocketChannel.configureBlocking(net.blockingAccept());
    ServerSocket socket = serverSocketChannel.socket();
    Log.info("!Starting server", "!address", net.address(), "!port", net.port(), "I/O workers", net.workers(), "sync", net.syncBufs(), "accept", blockingInfo);
    InetSocketAddress addr = new InetSocketAddress(net.address(), net.port());
    socket.setReceiveBufferSize(16 * 1024);
    socket.setReuseAddress(true);
    socket.bind(addr, MAX_PENDING_CONNECTIONS);
    Log.debug("Opened server socket", "address", addr);
    if (!net.blockingAccept()) {
      Log.debug("Registering accept selector");
      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    }
    initWorkers();
  } else {
    throw U.rte("Cannot open socket!");
  }
}

相关文章