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

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

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

Socket.setSoLinger介绍

[英]Sets this socket's SocketOptions#SO_LINGER timeout in seconds. If on is false, timeout is irrelevant.
[中]设置此套接字的SocketOptions#以便#以秒为单位延迟超时。如果on为false,则与超时无关。

代码示例

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

/**
 * create a socket channel.
 * @return the created socket channel
 * @throws IOException
 */
SocketChannel createSock() throws IOException {
  SocketChannel sock;
  sock = SocketChannel.open();
  sock.configureBlocking(false);
  sock.socket().setSoLinger(false, -1);
  sock.socket().setTcpNoDelay(true);
  return sock;
}

代码示例来源: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: 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: apache/zookeeper

try {
  sock = new Socket(addr.getHostString(), addr.getPort());
  sock.setSoLinger(false, -1);
  sock.setSoTimeout(1000);
  sock.setTcpNoDelay(true);
  sock.getOutputStream().write("isro".getBytes());
  sock.getOutputStream().flush();

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

@Override
  public Socket createSocket(String host, int port) throws IOException {
    Socket socket = rmiClientSocketFactory.createSocket(host, port);
    socket.setSoTimeout( timeoutMillis );
    socket.setSoLinger( false, 0 );
    return socket;
  }
}

代码示例来源:origin: alibaba/nacos

channel.configureBlocking(false);
channel.socket().setSoLinger(false, -1);
channel.socket().setReuseAddress(true);
channel.socket().setKeepAlive(true);
channel.socket().setTcpNoDelay(true);

代码示例来源:origin: jboss.remoting/jboss-remoting

protected void configureSocket(Socket s) throws SocketException
{
 s.setReuseAddress(getReuseAddress());
 
 if (keepAliveSet)           s.setKeepAlive(keepAlive);
 if (oOBInlineSet)           s.setOOBInline(oOBInline);
 if (receiveBufferSize > -1) s.setReceiveBufferSize(receiveBufferSize);
 if (sendBufferSize > -1)    s.setSendBufferSize(sendBufferSize);
 if (soLingerSet && 
    soLingerDuration > 0) s.setSoLinger(soLinger, soLingerDuration);
 if (trafficClass > -1)      s.setTrafficClass(trafficClass);
}

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

/**
 * Creates a socket to {@code dest}, and assigns it to ping_sock. Also assigns ping_input
 */
protected boolean setupPingSocket(IpAddress dest) {
  lock.lock();
  try {
    SocketAddress destAddr=new InetSocketAddress(dest.getIpAddress(), dest.getPort());
    ping_sock=getSocketFactory().createSocket("jgroups.fd.ping_sock");
    Util.bind(ping_sock, bind_addr, client_bind_port, client_bind_port+port_range);
    ping_sock.setSoLinger(true, 1);
    ping_sock.setKeepAlive(keep_alive);
    Util.connect(ping_sock, destAddr, sock_conn_timeout);
    ping_input=ping_sock.getInputStream();
    return true;
  }
  catch(Throwable ex) {
    if(!shuttin_down)
      log.debug("%s: failed connecting to %s: %s",
           local_addr, ping_dest != null? ping_dest : dest, ex.getMessage());
    return false;
  }
  finally {
    lock.unlock();
  }
}

代码示例来源: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: org.apache.zookeeper/zookeeper

try {
  sock = new Socket(addr.getHostName(), addr.getPort());
  sock.setSoLinger(false, -1);
  sock.setSoTimeout(1000);
  sock.setTcpNoDelay(true);
  sock.getOutputStream().write("isro".getBytes());
  sock.getOutputStream().flush();

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

public static void dump(String host, int port) {
  Socket s = null;
  try {
    byte[] reqBytes = new byte[4];
    ByteBuffer req = ByteBuffer.wrap(reqBytes);
    req.putInt(ByteBuffer.wrap("dump".getBytes()).getInt());
    s = new Socket();
    s.setSoLinger(false, 10);
    s.setSoTimeout(20000);
    s.connect(new InetSocketAddress(host, port));
    InputStream is = s.getInputStream();
    OutputStream os = s.getOutputStream();
    os.write(reqBytes);
    byte[] resBytes = new byte[1024];
    int rc = is.read(resBytes);
    String retv = new String(resBytes);
    System.out.println("rc=" + rc + " retv=" + retv);
  } catch (IOException e) {
    LOG.warn("Unexpected exception", e);
  } finally {
    if (s != null) {
      try {
        s.close();
      } catch (IOException e) {
        LOG.warn("Unexpected exception", e);
      }
    }
  }
}

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

channel.configureBlocking(false);
if (optionMap.contains(Options.TCP_OOB_INLINE)) channel.socket().setOOBInline(optionMap.get(Options.TCP_OOB_INLINE, false));
if (optionMap.contains(Options.TCP_NODELAY)) channel.socket().setTcpNoDelay(optionMap.get(Options.TCP_NODELAY, false));
if (optionMap.contains(Options.IP_TRAFFIC_CLASS)) channel.socket().setTrafficClass(optionMap.get(Options.IP_TRAFFIC_CLASS, -1));
if (optionMap.contains(Options.CLOSE_ABORT)) channel.socket().setSoLinger(optionMap.get(Options.CLOSE_ABORT, false), 0);
if (optionMap.contains(Options.KEEP_ALIVE)) channel.socket().setKeepAlive(optionMap.get(Options.KEEP_ALIVE, false));
if (optionMap.contains(Options.SEND_BUFFER)) channel.socket().setSendBufferSize(optionMap.get(Options.SEND_BUFFER, -1));
final SelectionKey selectionKey = WorkerThread.this.registerChannel(channel);

代码示例来源: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;
}

代码示例来源:origin: org.apache.zookeeper/zookeeper

/**
 * create a socket channel.
 * @return the created socket channel
 * @throws IOException
 */
SocketChannel createSock() throws IOException {
  SocketChannel sock;
  sock = SocketChannel.open();
  sock.configureBlocking(false);
  sock.socket().setSoLinger(false, -1);
  sock.socket().setTcpNoDelay(true);
  return sock;
}

代码示例来源:origin: jboss.remoting/jboss-remoting

protected void configureSocket(Socket s) throws SocketException
{
 s.setReuseAddress(getReuseAddress());
 
 if (keepAliveSet)           s.setKeepAlive(keepAlive);
 if (oOBInlineSet)           s.setOOBInline(oOBInline);
 if (receiveBufferSize > -1) s.setReceiveBufferSize(receiveBufferSize);
 if (sendBufferSize > -1)    s.setSendBufferSize(sendBufferSize);
 if (soLingerSet && 
    soLingerDuration > 0) s.setSoLinger(soLinger, soLingerDuration);
 if (trafficClass > -1)      s.setTrafficClass(trafficClass);
}

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

protected void setSocketParameters(Socket client_sock) throws SocketException {
  try {
    client_sock.setSendBufferSize(server.send_buf_size);
  }
  catch(IllegalArgumentException ex) {
    server.log.error("%s: exception setting send buffer to %d bytes: %s", server.local_addr, server.send_buf_size, ex);
  }
  try {
    client_sock.setReceiveBufferSize(server.recv_buf_size);
  }
  catch(IllegalArgumentException ex) {
    server.log.error("%s: exception setting receive buffer to %d bytes: %s", server.local_addr, server.recv_buf_size, ex);
  }
  client_sock.setKeepAlive(true);
  client_sock.setTcpNoDelay(server.tcp_nodelay);
  if(server.linger > 0)
    client_sock.setSoLinger(true, server.linger);
  else
    client_sock.setSoLinger(false, -1);
}

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

/**
 * 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
 */
protected void prepareSocket(Socket sock, HttpContext context,
               HttpParams params)
  throws IOException {
  // context currently not used, but derived classes may need it
  //@@@ is context allowed to be null?
  sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
  sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
  int linger = HttpConnectionParams.getLinger(params);
  if (linger >= 0) {
    sock.setSoLinger(linger > 0, linger);
  }
} // prepareSocket

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

public static void stat(String host, int port) {
  Socket s = null;
  try {
    byte[] reqBytes = new byte[4];
    ByteBuffer req = ByteBuffer.wrap(reqBytes);
    req.putInt(ByteBuffer.wrap("stat".getBytes()).getInt());
    s = new Socket();
    s.setSoLinger(false, 10);
    s.setSoTimeout(20000);
    s.connect(new InetSocketAddress(host, port));
    InputStream is = s.getInputStream();
    OutputStream os = s.getOutputStream();
    os.write(reqBytes);
    byte[] resBytes = new byte[1024];
    int rc = is.read(resBytes);
    String retv = new String(resBytes);
    System.out.println("rc=" + rc + " retv=" + retv);
  } catch (IOException e) {
    LOG.warn("Unexpected exception", e);
  } finally {
    if (s != null) {
      try {
        s.close();
      } catch (IOException e) {
        LOG.warn("Unexpected exception", e);
      }
    }
  }
}

代码示例来源:origin: org.apache.thrift/libthrift

private TNonblockingSocket(SocketChannel socketChannel, int timeout, SocketAddress socketAddress)
  throws IOException {
 socketChannel_ = socketChannel;
 socketAddress_ = socketAddress;
 // make it a nonblocking channel
 socketChannel.configureBlocking(false);
 // set options
 Socket socket = socketChannel.socket();
 socket.setSoLinger(false, 0);
 socket.setTcpNoDelay(true);
 socket.setKeepAlive(true);
 setTimeout(timeout);
}

代码示例来源:origin: didi/DDMQ

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();
}

相关文章