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

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

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

Socket.setReuseAddress介绍

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

代码示例

代码示例来源:origin: aws/aws-sdk-java

@Override
public void setReuseAddress(boolean on) throws SocketException {
  sock.setReuseAddress(on);
}

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

@Override
public SocketChannelConfig setReuseAddress(boolean reuseAddress) {
  try {
    javaSocket.setReuseAddress(reuseAddress);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
  return this;
}

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

@Override
public SocketChannelConfig setReuseAddress(boolean reuseAddress) {
  try {
    javaSocket.setReuseAddress(reuseAddress);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
  return this;
}

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

/**
 * See {@link Socket#setReuseAddress(boolean)}. Calling this method does not trigger mode detection.
 */
@Override
public void setReuseAddress(boolean on) throws SocketException {
  getSocketAllowUnknownMode().setReuseAddress(on);
}

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

@Override
public SocketChannelConfig setReuseAddress(boolean reuseAddress) {
  try {
    javaSocket.setReuseAddress(reuseAddress);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
  return this;
}

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

public void setReuseAddress(boolean reuseAddress) {
  try {
    socket.setReuseAddress(reuseAddress);
  } catch (SocketException e) {
    throw new ChannelException(e);
  }
}

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

public SocketChannel getTCPSocket() throws IOException {
 // Under lock, claim an existing open socket if possible
 synchronized(this) {
  // Limit myself to the number of open sockets from node-to-node
  while( _socksAvail == 0 )
   try { wait(); } catch( InterruptedException ie ) { }
  // Claim an open socket
  SocketChannel sock = _socks[--_socksAvail];
  if( sock != null ) {
   if( sock.isOpen() ) return sock; // Return existing socket!
   // Else its an already-closed socket, lower open TCP count
   assert TCPS.get() > 0;
   TCPS.decrementAndGet();
  }
 }
 // Must make a fresh socket
 SocketChannel sock2 = SocketChannel.open();
 sock2.socket().setReuseAddress(true);
 sock2.socket().setSendBufferSize(AutoBuffer.BBSIZE);
 boolean res = sock2.connect( _key );
 assert res && !sock2.isConnectionPending() && sock2.isBlocking() && sock2.isConnected() && sock2.isOpen();
 TCPS.incrementAndGet();     // Cluster-wide counting
 return sock2;
}
public synchronized void freeTCPSocket( SocketChannel sock ) {

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

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

代码示例来源:origin: sohutv/cachecloud

public void connect() {
 if (!isConnected()) {
  try {
   socket = new Socket();
   // ->@wjw_add
   socket.setReuseAddress(true);
   socket.setKeepAlive(true); // Will monitor the TCP connection is
   // valid
   socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
   // ensure timely delivery of data
   socket.setSoLinger(true, 0); // Control calls close () method,
   // the underlying socket is closed
   // immediately
   // <-@wjw_add
   socket.connect(new InetSocketAddress(host, port), connectionTimeout);
   socket.setSoTimeout(soTimeout);
   outputStream = new RedisOutputStream(socket.getOutputStream());
   inputStream = new RedisInputStream(socket.getInputStream());
  } catch (IOException ex) {
   UsefulDataCollector.collectException(ex, getHostPort(), System.currentTimeMillis());
   
   broken = true;
   throw new JedisConnectionException(ex);
  }
 }
}

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

try {
  s = new Socket();
  s.setReuseAddress( true );
  SocketAddress sa = new InetSocketAddress( value.getPublicIpAddress(), 22 );
  s.connect( sa, 2000 );

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

socket.setReuseAddress( isReuseAddr() );

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

channel.socket().setReuseAddress(true);
channel.socket().setKeepAlive(true);
channel.socket().setTcpNoDelay(true);

代码示例来源:origin: scouter-project/scouter

public void open(int serverId) {
  close();
  Server server = ServerManager.getInstance().getServer(serverId);
  if (server == null) {
    return;
  }
  try {
    socket = new Socket();
    socket.setKeepAlive(false);
    socket.setTcpNoDelay(true);
    socket.setPerformancePreferences(0, 2, 1);
    socket.setReuseAddress(true);
    socket.setSoLinger(true, 1000);
    socket.connect(new InetSocketAddress(server.getIp(), server.getPort()),3000);
    socket.setSoTimeout(server.getSoTimeOut());
    in = new DataInputX(new BufferedInputStream(socket.getInputStream()));
    out = new DataOutputX(new BufferedOutputStream(socket.getOutputStream()));
    
    out.writeInt(NetCafe.TCP_CLIENT);
    out.flush();
    log.info("Connected {} to {}:{}", this, server.getIp(), server.getPort());
  } catch (Throwable t) {
    log.error(t.getMessage());
    close();
  }
}

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

socketChannel.socket().setReuseAddress(serverConfig.getReuseAddress());

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

socketChannel.socket().setReuseAddress(serverConfig.getReuseAddress());

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

socketChannel.socket().setReuseAddress(clientConfig.getReuseAddress());

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

socketChannel.socket().setReuseAddress(clientConfig.getReuseAddress());

相关文章