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

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

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

Socket.isConnected介绍

[英]Returns whether this socket is connected to a remote host.
[中]返回此套接字是否连接到远程主机。

代码示例

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

@Override
public boolean isConnected () {
  if (socket != null) {
    return socket.isConnected();
  } else {
    return false;
  }
}

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

@Override
public boolean isConnected () {
  if (socket != null) {
    return socket.isConnected();
  } else {
    return false;
  }
}

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

public boolean isConnected() {
  Socket socket = this.socket;
  if (socket != null) {
    return socket.isConnected();
  }
  return false;
}

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

@Override
public boolean isActive() {
  return !socket.isClosed() && socket.isConnected();
}

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

@Override
public boolean isConnected() {
  return sock.isConnected();
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public boolean isConnected()
{
  if( sock == null )
    return false;
  return sock.isConnected();
}

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

@Override
public boolean isActive() {
  return !socket.isClosed() && socket.isConnected();
}

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

@Override
public boolean isActive() {
  return !socket.isClosed() && socket.isConnected();
}

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

/**
 * Returns the remote address and port of this socket as a {@code
 * SocketAddress} or null if the socket is not connected.
 *
 * @return the remote socket address and port.
 */
public SocketAddress getRemoteSocketAddress() {
  if (!isConnected()) {
    return null;
  }
  return new InetSocketAddress(getInetAddress(), getPort());
}

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

/**
 * See {@link Socket#isConnected()}. Calling this method does not trigger mode detection.
 */
@Override
public boolean isConnected() {
  return getSocketAllowUnknownMode().isConnected();
}

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

Socket s = new Socket();

System.out.println("isConnected: " + s.isConnected() +
         " isBound: "     + s.isBound() +
         " isClosed: "    + s.isClosed());

s.connect(new InetSocketAddress("google.com", 80));

System.out.println("isConnected: " + s.isConnected() +
          " isBound: "    + s.isBound() +
          " isClosed: "   + s.isClosed());

s.close();

System.out.println("isConnected: " + s.isConnected() +
          " isBound: "    + s.isBound() +
          " isClosed: "   + s.isClosed());

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

/**
 * Returns the port number of the target host this socket is connected to, or 0 if this socket
 * is not yet connected.
 */
public int getPort() {
  if (!isConnected()) {
    return 0;
  }
  return impl.getPort();
}

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

@Override
  public void disconnect() throws IOException
  {
    if ( socket != null && socket.isConnected() )
    {
      socket.close();
    }
  }
}

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

/**
 * Create a new instance from the given {@link Socket}
 *
 * @param parent    the parent {@link Channel} which was used to create this instance. This can be null if the
 *                  {@link} has no parent as it was created by your self.
 * @param socket    the {@link Socket} which is used by this instance
 */
public OioSocketChannel(Channel parent, Socket socket) {
  super(parent);
  this.socket = socket;
  config = new DefaultOioSocketChannelConfig(this, socket);
  boolean success = false;
  try {
    if (socket.isConnected()) {
      activate(socket.getInputStream(), socket.getOutputStream());
    }
    socket.setSoTimeout(SO_TIMEOUT);
    success = true;
  } catch (Exception e) {
    throw new ChannelException("failed to initialize a socket", e);
  } finally {
    if (!success) {
      try {
        socket.close();
      } catch (IOException e) {
        logger.warn("Failed to close a socket.", e);
      }
    }
  }
}

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

public boolean isConnected() {
 return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected()
   && !socket.isInputShutdown() && !socket.isOutputShutdown();
}

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

public TcpConnection(Socket s, TcpServer server) throws Exception {
  this.sock=s;
  this.server=server;
  if(s == null)
    throw new IllegalArgumentException("Invalid parameter s=" + s);
  setSocketParameters(s);
  this.out=new DataOutputStream(createBufferedOutputStream(s.getOutputStream()));
  this.in=new DataInputStream(createBufferedInputStream(s.getInputStream()));
  this.connected=sock.isConnected();
  this.peer_addr=server.usePeerConnections()? readPeerAddress(s)
   : new IpAddress((InetSocketAddress)s.getRemoteSocketAddress());
  last_access=getTimestamp(); // last time a message was sent or received (ns)
}

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

@Override
  public void run() {
    try {
      DataInputStream input = new DataInputStream(socket.getInputStream());
      DataOutputStream output = new DataOutputStream(socket.getOutputStream());
      while (socket.isConnected() && !socket.isClosed()) {
        int size = input.readInt();
        if (renegotiate.get()) {
          renegotiate.set(false);
          ((SSLSocket) socket).startHandshake();
        }
        byte[] bytes = new byte[size];
        input.readFully(bytes);
        output.writeInt(size);
        output.write(bytes);
        output.flush();
      }
    } catch (IOException e) {
      // ignore
    } finally {
      try {
        socket.close();
      } catch (IOException e) {
        // ignore
      }
    }
  }
};

代码示例来源:origin: k9mail/k-9

private void createMocks()
    throws MessagingException, IOException, NoSuchAlgorithmException, KeyManagementException {
  mockTrustedSocketFactory = mock(TrustedSocketFactory.class);
  mockSocket = mock(Socket.class);
  outputStreamForMockSocket = new ByteArrayOutputStream();
  when(mockTrustedSocketFactory.createSocket(null, host, port, null))
      .thenReturn(mockSocket);
  when(mockSocket.getOutputStream()).thenReturn(outputStreamForMockSocket);
  when(mockSocket.isConnected()).thenReturn(true);
}

代码示例来源:origin: k9mail/k-9

@Before
public void setUp() throws Exception {
  ServerSettings serverSettings = createServerSettings();
  when(mockStoreConfig.getInboxFolder()).thenReturn(Pop3Folder.INBOX);
  when(mockTrustedSocketFactory.createSocket(null, "server", 12345, null)).thenReturn(mockSocket);
  when(mockSocket.isConnected()).thenReturn(true);
  when(mockSocket.isClosed()).thenReturn(false);
  when(mockSocket.getOutputStream()).thenReturn(mockOutputStream);
  store = new Pop3Store(serverSettings, mockStoreConfig, mockTrustedSocketFactory);
}

代码示例来源:origin: k9mail/k-9

@Test(expected = MessagingException.class)
public void open_whenSocketNotConnected_throwsMessagingException() throws Exception {
  when(mockSocket.isConnected()).thenReturn(false);
  addSettingsForValidMockSocket();
  settings.setAuthType(AuthType.PLAIN);
  Pop3Connection connection = new Pop3Connection(settings, mockTrustedSocketFactory);
  connection.open();
}

相关文章