本文整理汇总了Java中java.net.Socket.isClosed方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.isClosed方法的具体用法?Java Socket.isClosed怎么用?Java Socket.isClosed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.Socket
的用法示例。
在下文中一共展示了Socket.isClosed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isSecure
import java.net.Socket; //导入方法依赖的package包/类
/**
* Checks whether a socket connection is secure.
* This factory creates TLS/SSL socket connections
* which, by default, are considered secure.
* <br/>
* Derived classes may override this method to perform
* runtime checks, for example based on the cypher suite.
*
* @param sock the connected socket
* @return <code>true</code>
* @throws IllegalArgumentException if the argument is invalid
*/
public boolean isSecure(final Socket sock) throws IllegalArgumentException
{
if (sock == null)
{
throw new IllegalArgumentException("Socket may not be null");
}
// This instanceof check is in line with createSocket() above.
if (!(sock instanceof SSLSocket))
{
throw new IllegalArgumentException("Socket not created by this factory");
}
// This check is performed last since it calls the argument object.
if (sock.isClosed())
{
throw new IllegalArgumentException("Socket is closed");
}
return true;
}
示例2: run
import java.net.Socket; //导入方法依赖的package包/类
public void run() {
while(!server.isClosed()){
Socket sk = null;
try {
sk = server.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();logger.error("Exception",e);
}
if(sk!=null&&!sk.isClosed()){
NodeReceiveThread nrt= new NodeReceiveThread(sk);
nrt.start();
}
}
System.out.println("server closed");
logger.info("server closed");
}
示例3: isSecure
import java.net.Socket; //导入方法依赖的package包/类
/**
* Checks whether a socket connection is secure.
* This factory creates plain socket connections
* which are not considered secure.
*
* @param sock the connected socket
*
* @return <code>false</code>
*
* @throws IllegalArgumentException if the argument is invalid
*/
public final boolean isSecure(Socket sock)
throws IllegalArgumentException {
if (sock == null) {
throw new IllegalArgumentException("Socket may not be null.");
}
// This class check assumes that createSocket() calls the constructor
// directly. If it was using javax.net.SocketFactory, we couldn't make
// an assumption about the socket class here.
if (sock.getClass() != Socket.class) {
throw new IllegalArgumentException
("Socket not created by this factory.");
}
// This check is performed last since it calls a method implemented
// by the argument object. getClass() is final in java.lang.Object.
if (sock.isClosed()) {
throw new IllegalArgumentException("Socket is closed.");
}
return false;
}
示例4: testClosedSocketCloser
import java.net.Socket; //导入方法依赖的package包/类
/**
* Verify that a closed SocketCloser will still close an open socket
*/
@Test
public void testClosedSocketCloser() {
final AtomicBoolean runnableCalled = new AtomicBoolean();
Runnable r = new Runnable() {
@Override
public void run() {
runnableCalled.set(true);
}
};
final Socket s = createClosableSocket();
this.socketCloser.close();
this.socketCloser.asyncClose(s, "A", r);
WaitCriterion wc = new WaitCriterion() {
public boolean done() {
return runnableCalled.get() && s.isClosed();
}
public String description() {
return "runnable was not called or socket was not closed";
}
};
Wait.waitForCriterion(wc, 5000, 10, true);
}
示例5: isSecure
import java.net.Socket; //导入方法依赖的package包/类
@Override
public boolean isSecure(Socket sock) throws IllegalArgumentException {
if (sock == null) {
throw new IllegalArgumentException("Socket may not be null.");
}
if (!(sock instanceof SSLSocket)) {
throw new IllegalArgumentException("Socket not created by this factory.");
}
if (sock.isClosed()) {
throw new IllegalArgumentException("Socket is closed.");
}
return true;
}
示例6: NetworkConnection
import java.net.Socket; //导入方法依赖的package包/类
/**
* Creates a connection using a connected socket
*
* @param socket the socket this connection is based on
* @throws IOException if the socket is either closed, unbound or not connected.
*/
public NetworkConnection(Socket socket) throws IOException {
if (socket.isClosed() || !socket.isBound() || !socket.isConnected())
throw new IOException("NetworkConnection expects a not closed, bound and connected socket");
this.socket = socket;
}
示例7: Client
import java.net.Socket; //导入方法依赖的package包/类
public Client(final String ip, int port) throws UnknownHostException,
IOException, ClassNotFoundException {
Socket socket = new Socket(ip, port);
ObjectOutputStream outToClient = new ObjectOutputStream(
socket.getOutputStream());
ObjectInputStream inFromClient = new ObjectInputStream(
socket.getInputStream());
long startTime = System.currentTimeMillis();
long currentTime = System.currentTimeMillis();
while (socket.isConnected() && jobs < MAX_JOBS
&& (currentTime - startTime) < MAX_RUNTIME) {
jobs++;
ResultRunnable job = (ResultRunnable) inFromClient.readObject();
job.setIPAdress(ip);
job.run();
Object result = job.getResult();
try {
outToClient.writeObject(result);
} catch (NotSerializableException e) {
outToClient.writeObject(e);
}
currentTime = System.currentTimeMillis();
}
if (!socket.isClosed()) {
restart = true;
}
socket.close();
}
示例8: isHealthy
import java.net.Socket; //导入方法依赖的package包/类
boolean isHealthy(boolean doExtensiveChecks) {
if (!connected) {
return false;
}
Socket socket;
BufferedSource source;
synchronized (this) {
socket = this.socket;
source = this.source;
}
if (socket == null ||
source == null ||
socket.isClosed() ||
socket.isInputShutdown() ||
socket.isOutputShutdown()) {
return false;
}
if (doExtensiveChecks) {
try {
int readTimeout = socket.getSoTimeout();
try {
socket.setSoTimeout(1);
return !source.exhausted();
} finally {
socket.setSoTimeout(readTimeout);
}
} catch (SocketTimeoutException ignored) {
ignored.printStackTrace();
// Read timed out; socket is good.
} catch (IOException e) {
return false; // Couldn't read; socket is closed.
}
}
return true;
}
示例9: closeSocket
import java.net.Socket; //导入方法依赖的package包/类
private void closeSocket(Socket socket) {
try {
if (!socket.isClosed()) {
socket.close();
}
} catch (IOException e) {
onError(new ProxyCacheException("Error closing socket", e));
}
}
示例10: run
import java.net.Socket; //导入方法依赖的package包/类
@Override
public void run() {
List<Socket> sockets = new ArrayList<Socket>();
while (this.tokenResponse == null) {
Socket s;
try {
s = this.socket.accept();
sockets.add(s);
InternalWebServerTransaction c = new InternalWebServerTransaction(s,this, nonce.toString());
c.addObserver(this);
} catch (IOException e) {
if(e instanceof SocketException) {
// When user obtain a token closing listen socket cause this exception
// In this case this is a normal exception
logger.info("Closing listening socket.");
} else {
logger.error("IOException while accepting client requests", e);
}
// Close all transcation sockets
for(Socket transactionSocket: sockets) {
try {
if(!transactionSocket.isClosed()) {
transactionSocket.close();
}
} catch (IOException e1) {
logger.error("IOException while closing client requests", e1);
}
}
serverThread.interrupt();
}
}
serverThread.interrupt();
}
示例11: request
import java.net.Socket; //导入方法依赖的package包/类
public static Packet request(String host, int port, Packet packet) {
KeyPair keyPair = Crypter.generateKeyPair();
try {
Socket socket = new Socket(host, port);
if (socket.isClosed()) {
return new ErrorPacket("socket closed");
}
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
out.writeObject(keyPair.getPublic());
PublicKey publicKey = (PublicKey) in.readObject();
SecretKey key = Crypter.generateEC(keyPair.getPrivate(), publicKey);
out.writeObject(Crypter.encrypt(key, Crypter.toByteArray(packet)));
out.flush();
Packet response = (Packet) Crypter.toObject(Crypter.decrypt(key, (byte[]) in.readObject()));
in.close();
out.close();
socket.close();
return response;
} catch (Exception ex) {
return new ErrorPacket(ex.getMessage());
}
}
示例12: isSecure
import java.net.Socket; //导入方法依赖的package包/类
/**
* Checks whether a socket connection is secure.
* This factory creates plain socket connections
* which are not considered secure.
*
* @param sock the connected socket
*
* @return <code>false</code>
*
* @throws IllegalArgumentException if the argument is invalid
*/
public final boolean isSecure(Socket sock)
throws IllegalArgumentException {
if (sock == null) {
throw new IllegalArgumentException("Socket may not be null.");
}
// This check is performed last since it calls a method implemented
// by the argument object. getClass() is final in java.lang.Object.
if (sock.isClosed()) {
throw new IllegalArgumentException("Socket is closed.");
}
return false;
}
示例13: isSecure
import java.net.Socket; //导入方法依赖的package包/类
/**
* Checks whether a socket connection is secure.
* This factory creates TLS/SSL socket connections
* which, by default, are considered secure.
* <br/>
* Derived classes may override this method to perform
* runtime checks, for example based on the cypher suite.
*
* @param sock the connected socket
*
* @return <code>true</code>
*
* @throws IllegalArgumentException if the argument is invalid
*/
public boolean isSecure(final Socket sock) throws IllegalArgumentException {
if (sock == null) {
throw new IllegalArgumentException("Socket may not be null");
}
// This instanceof check is in line with createSocket() above.
if (!(sock instanceof SSLSocket)) {
throw new IllegalArgumentException("Socket not created by this factory");
}
// This check is performed last since it calls the argument object.
if (sock.isClosed()) {
throw new IllegalArgumentException("Socket is closed");
}
return true;
}
示例14: init
import java.net.Socket; //导入方法依赖的package包/类
private void init() throws Exception {
try {
socket = new Socket(targetHost, targetPort);
outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream, false);
InputStream inputStream = socket.getInputStream();
// send an HTTP request to the web server
out.println(String.format("SOURCE %s HTTP/1.0", mounter));
out.println(String.format("Authorization: Basic %s", HttpRequest.Base64.encode(user + ":" + password)));
out.println("User-Agent: libshout/2.3.1");
out.println(String.format("Content-Type: %s", mimeType.getContentType()));
out.println(String.format("ice-name: %s", iceName));
out.println("ice-public: 0");
if (iceDesc != null) {
out.println(String.format("ice-description: %s", iceDesc));
}
out.println();
out.flush();
// check if 404
LineReader lineReader = new LineReader(new InputStreamReader(inputStream));
String data = lineReader.readLine();
handleResponse(data);
} catch (Exception e) {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e1) {
// skip
}
}
throw e;
}
}
示例15: asyncClose
import java.net.Socket; //导入方法依赖的package包/类
/**
* Closes the specified socket in a background thread. In some cases we see close hang (see bug
* 33665). Depending on how the SocketCloser is configured (see ASYNC_CLOSE_WAIT_MILLISECONDS)
* this method may block for a certain amount of time. If it is called after the SocketCloser is
* closed then a normal synchronous close is done.
*
* @param sock the socket to close
* @param address identifies who the socket is connected to
* @param extra an optional Runnable with stuff to execute in the async thread
*/
public void asyncClose(final Socket sock, final String address, final Runnable extra) {
if (sock == null || sock.isClosed()) {
return;
}
boolean doItInline = false;
try {
synchronized (asyncCloseExecutors) {
if (isClosed()) {
// this SocketCloser has been closed so do a synchronous, inline, close
doItInline = true;
} else {
asyncExecute(address, new Runnable() {
public void run() {
Thread.currentThread().setName("AsyncSocketCloser for " + address);
try {
if (extra != null) {
extra.run();
}
inlineClose(sock);
} finally {
Thread.currentThread().setName("unused AsyncSocketCloser");
}
}
});
}
}
} catch (OutOfMemoryError ignore) {
// If we can't start a thread to close the socket just do it inline.
// See bug 50573.
doItInline = true;
}
if (doItInline) {
if (extra != null) {
extra.run();
}
inlineClose(sock);
}
}