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

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

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

Socket.isClosed介绍

[英]Returns whether this socket is closed.
[中]返回此套接字是否已关闭。

代码示例

代码示例来源:origin: CarGuo/GSYVideoPlayer

private void closeSocket(Socket socket) {
  try {
    if (!socket.isClosed()) {
      socket.close();
    }
  } catch (IOException e) {
    //onError(new ProxyCacheException("Error closing socket", e));
  }
}

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

public void flush() throws IOException {
  try {
    learner.writePacket(null, true);
  } catch(IOException e) {
    LOG.warn("Closing connection to leader, exception during packet send", e);
    try {
      if (!learner.sock.isClosed()) {
        learner.sock.close();
      }
    } catch (IOException e1) {
        // Nothing to do, we are shutting things down, so an exception here is irrelevant
        LOG.debug("Ignoring error closing the connection", e1);
    }
  }
}

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

private static void forceClose(Socket s) {
  if (s == null || s.isClosed()) {
    return;
  }
  try {
    s.close();
  } catch (IOException e) {
  }
}

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

private static void forceClose(Socket s) {
  if (s == null || s.isClosed()) {
    return;
  }
  try {
    s.close();
  } catch (IOException e) {
  }
}

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

private static void forceClose(Socket s) {
  if (s == null || s.isClosed()) {
    return;
  }
  try {
    s.close();
  } catch (IOException e) {
  }
}

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

public void flush() throws IOException {
  try {
    learner.writePacket(null, true);
  } catch(IOException e) {
    LOG.warn("Closing connection to leader, exception during packet send", e);
    try {
      if (!learner.sock.isClosed()) {
        learner.sock.close();
      }
    } catch (IOException e1) {
        // Nothing to do, we are shutting things down, so an exception here is irrelevant
        LOG.debug("Ignoring error closing the connection", e1);
    }
  }
}

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

private boolean closeSocket() {
  clearSelectionKey();
  SocketChannel channel = this.channel;
  if (channel != null) {
    boolean isSocketClosed = true;
    Socket socket = channel.socket();
    if (socket != null) {
      try {
        socket.close();
      } catch (Throwable e) {
      }
      isSocketClosed = socket.isClosed();
    }
    try {
      channel.close();
    } catch (Throwable e) {
    }
    return isSocketClosed && (!channel.isOpen());
  } else {
    return true;
  }
}

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

public void shutdown() {
  // Send the packet of death
  try {
    queuedPackets.put(proposalOfDeath);
  } catch (InterruptedException e) {
    LOG.warn("Ignoring unexpected exception", e);
  }
  try {
    if (sock != null && !sock.isClosed()) {
      sock.close();
    }
  } catch (IOException e) {
    LOG.warn("Ignoring unexpected exception during socket close", e);
  }
  this.interrupt();
  learnerMaster.removeLearnerHandler(this);
  learnerMaster.unregisterLearnerHandlerBean(this);
}

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

/**
 * 建立远程连接并发送命令
 *
 * @param command
 * @param server
 * @param port
 */
private static void doSend(String command, String server, int port) {
  Socket socket = null;
  try {
    socket = new Socket(server, port);
    OutputStream os = socket.getOutputStream();
    BufferedOutputStream out = new BufferedOutputStream(os);
    out.write(command.getBytes());
    out.write('\r');
    out.flush();
  } catch (UnknownHostException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (socket != null && !socket.isClosed()) {
      try {
        socket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

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

e.printStackTrace();
} finally {
  if (socket != null && !socket.isClosed()) {
    try {
      socket.close();
    } catch (IOException e) {
      e.printStackTrace();

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

public void shutdown() {
  // Send the packet of death
  try {
    queuedPackets.put(proposalOfDeath);
  } catch (InterruptedException e) {
    LOG.warn("Ignoring unexpected exception", e);
  }
  try {
    if (sock != null && !sock.isClosed()) {
      sock.close();
    }
  } catch (IOException e) {
    LOG.warn("Ignoring unexpected exception during socket close", e);
  }
  this.interrupt();
  leader.removeLearnerHandler(this);
}

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

public void processRequest(Request si) {
  if(si.type != OpCode.sync){
    QuorumPacket qp = new QuorumPacket(Leader.ACK, si.getHdr().getZxid(), null,
      null);
    try {
      learner.writePacket(qp, false);
    } catch (IOException e) {
      LOG.warn("Closing connection to leader, exception during packet send", e);
      try {
        if (!learner.sock.isClosed()) {
          learner.sock.close();
        }
      } catch (IOException e1) {
        // Nothing to do, we are shutting things down, so an exception here is irrelevant
        LOG.debug("Ignoring error closing the connection", e1);
      }
    }
  }
}

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

public static void closeQuietly(final Socket socket) {
  if (socket == null) {
    return;
  }
  try {
    try {
      // can't shudown input/output individually with secure sockets
      if ((socket instanceof SSLSocket) == false) {
        if (socket.isInputShutdown() == false) {
          socket.shutdownInput();
        }
        if (socket.isOutputShutdown() == false) {
          socket.shutdownOutput();
        }
      }
    } finally {
      if (socket.isClosed() == false) {
        socket.close();
      }
    }
  } catch (final Exception ex) {
    logger.debug("Failed to close socket due to: " + ex, ex);
  }
}

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

: policy.getSocketPolicy() == DISCONNECT_DURING_RESPONSE_BODY;
while (!socket.isClosed()) {
 for (int b = 0; b < bytesPerPeriod; ) {
   socket.close();
   return;

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

} finally {
  if (error && s != null && !s.isClosed()) {
    try {
      s.close();
    } catch (IOException e) {
      LOG.warn("Error closing socket", e);

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

if (socket.isClosed()) {
  logger.debug("Connection to Peer {} is closed. Will not attempt to communicate over this Socket.", peerDescription);
  continue;
if (socket != null) {
  try {
    socket.close();
  } catch (final IOException ioe) {
    e.addSuppressed(ioe);

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

public void processRequest(Request si) {
  if(si.type != OpCode.sync){
    QuorumPacket qp = new QuorumPacket(Leader.ACK, si.hdr.getZxid(), null,
      null);
    try {
      learner.writePacket(qp, false);
    } catch (IOException e) {
      LOG.warn("Closing connection to leader, exception during packet send", e);
      try {
        if (!learner.sock.isClosed()) {
          learner.sock.close();
        }
      } catch (IOException e1) {
        // Nothing to do, we are shutting things down, so an exception here is irrelevant
        LOG.debug("Ignoring error closing the connection", e1);
      }
    }
  }
}

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

@Override
 public void run() {
  Socket client = null;
  try {
   client = serverSocket.accept();
   Thread.sleep(12000);
  } catch (InterruptedException e) {
  } catch (IOException e) {
  } finally {
   if (client != null && !client.isClosed()) {
    try {
     client.close();
    } catch (IOException e) {
    }
   }
  }
 }
};

相关文章