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

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

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

Socket.setKeepAlive介绍

[英]Sets this socket's SocketOptions#SO_KEEPALIVE option.
[中]设置此套接字的SocketOptions#SO_KEEPALIVE选项。

代码示例

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

/**
 * Helper method to set socket options.
 *
 * @param sock
 *            Reference to socket
 */
private void setSockOpts(Socket sock) throws SocketException {
  sock.setTcpNoDelay(true);
  sock.setKeepAlive(tcpKeepAlive);
  sock.setSoTimeout(self.tickTime * self.syncLimit);
}

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

private void createConnection() throws IOException {
  client = new Socket(hostName, port);
  client.setKeepAlive(true);
  client.setTcpNoDelay(true);
  outputStream = client.getOutputStream();
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

/**
 * 激活网络
 */
private Socket connect(InetSocketAddress address) throws IOException {
  final Socket socket = new Socket();
  socket.setSoTimeout(0);
  socket.connect(address, _1MIN);
  socket.setKeepAlive(true);
  socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  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: voldemort/voldemort

+ socketChannel.socket().getPort());
socketChannel.socket().setTcpNoDelay(true);
socketChannel.socket().setReuseAddress(true);
socketChannel.socket().setKeepAlive(socketKeepAlive);
socketChannel.socket().setSendBufferSize(socketBufferSize);

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

@Nullable @Override public Object call() throws Exception {
    ServerSocket srvSock = null;
    Socket sock = null;
    try {
      srvSock = new ServerSocket(60000, 0, addr);
      sock = srvSock.accept();
      X.println("Socket [timeout=" + sock.getSoTimeout() + ", linger=" + sock.getSoLinger() +
        ", sndBuf=" + sock.getSendBufferSize() + ", sndBuf=" + sock.getSendBufferSize() + ']');
      sock.setKeepAlive(true);
      sock.setSoTimeout(2000);
      sock.setSendBufferSize(256 * 1024);
      X.println("Socket [timeout=" + sock.getSoTimeout() + ", linger=" + sock.getSoLinger() +
        ", sndBuf=" + sock.getSendBufferSize() + ", rcvBuf=" + sock.getReceiveBufferSize() + ']');
      while (!done.get())
        X.println("Read from socket: " + sock.getInputStream().read());
      return null;
    }
    finally {
      U.closeQuiet(srvSock);
      U.closeQuiet(sock);
    }
  }
},

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

public static BioSocketChannel open(SocketAddress address) throws Exception {
  Socket socket = new Socket();
  socket.setSoTimeout(BioSocketChannel.SO_TIMEOUT);
  socket.setTcpNoDelay(true);
  socket.setKeepAlive(true);
  socket.setReuseAddress(true);
  socket.connect(address, BioSocketChannel.DEFAULT_CONNECT_TIMEOUT);
  return new BioSocketChannel(socket);
}

代码示例来源:origin: igniterealtime/Openfire

socket.setSoTimeout(0);
socket.setKeepAlive(true);
initialHSStatus = HandshakeStatus.NEED_WRAP;
tlsEngine.beginHandshake();

代码示例来源:origin: jenkinsci/jenkins

@Override
public void run() {
  try {
    // the loop eventually terminates when the socket is closed.
    while (!shuttingDown) {
      Socket s = serverSocket.accept().socket();
      // this prevents a connection from silently terminated by the router in between or the other peer
      // and that goes without unnoticed. However, the time out is often very long (for example 2 hours
      // by default in Linux) that this alone is enough to prevent that.
      s.setKeepAlive(true);
      // we take care of buffering on our own
      s.setTcpNoDelay(true);
      new ConnectionHandler(s, new ConnectionHandlerFailureCallback(this) {
        @Override
        public void run(Throwable cause) {
          LOGGER.log(Level.WARNING, "Connection handler failed, restarting listener", cause);
          shutdown();
          TcpSlaveAgentListenerRescheduler.schedule(getParentThread(), cause);
        }
      }).start();
    }
  } catch (IOException e) {
    if(!shuttingDown) {
      LOGGER.log(Level.SEVERE,"Failed to accept TCP connections", 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: wildfly/wildfly

socket.setKeepAlive(keepAlive != 0);
socket.setOOBInline(oobInline != 0);
socket.setTcpNoDelay(tcpNoDelay != 0);
final int sendBuffer = this.sendBuffer;
if (sendBuffer > 0) socket.setSendBufferSize(sendBuffer);
final WorkerThread ioThread = worker.getIoThread(hash);
final SelectionKey selectionKey = ioThread.registerChannel(accepted);

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

@Nullable @Override public Object call() throws Exception {
    Socket sock = null;
    try {
      sock = new Socket(addr, 60000);
      X.println("Socket [timeout=" + sock.getSoTimeout() + ", linger=" + sock.getSoLinger() +
        ", sndBuf=" + sock.getSendBufferSize() + ", sndBuf=" + sock.getSendBufferSize() + ']');
      sock.setKeepAlive(true);
      sock.setSoTimeout(2000);
      sock.setSendBufferSize(256 * 1024);
      X.println("Socket [timeout=" + sock.getSoTimeout() + ", linger=" + sock.getSoLinger() +
        ", sndBuf=" + sock.getSendBufferSize() + ", sndBuf=" + sock.getSendBufferSize() + ']');
      int i = 0;
      while (!done.get()) {
        sock.getOutputStream().write(++i);
        sock.getOutputStream().flush();
        X.println("Wrote to socket: " + i);
        X.println("Socket connected: " + sock.isConnected());
        X.println("Socket keep alive: " + sock.getKeepAlive());
        U.sleep(1000);
      }
      return null;
    }
    finally {
      U.closeQuiet(sock);
    }
  }
},

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

相关文章

微信公众号

最新文章

更多