Java Code Examples for java.net.Socket#isClosed()

The following examples show how to use java.net.Socket#isClosed() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: p4ic4idea   File: RpcSocketPool.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Release a socket back to the pool as no longer using
 * 
 * @param socket
 * @param shutdownHandler
 * @throws IOException
 */
public void release(Socket socket, ShutdownHandler shutdownHandler)
		throws IOException {
	if (isAlive(socket)) {
		boolean close = false;
		synchronized (this.pool) {
			if (this.pool.size() < size) {
				this.pool.add(new SocketEntry(socket));
			} else {
				close = true;
			}
		}
		if (close) {
			if (shutdownHandler != null) {
				shutdownHandler.shutdown(socket);
			}
			if (!socket.isClosed()) {
				socket.getInputStream().close();
			}
			if (!socket.isClosed()) {
				socket.getOutputStream().close();
			}
			socket.close();
		}
	}
}
 
Example 2
Source Project: p4ic4idea   File: RpcSocketPool.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Release a socket back to the pool as no longer using
 * 
 * @param socket
 * @param shutdownHandler
 * @throws IOException
 */
public void release(Socket socket, ShutdownHandler shutdownHandler)
		throws IOException {
	if (isAlive(socket)) {
		boolean close = false;
		synchronized (this.pool) {
			if (this.pool.size() < size) {
				this.pool.add(new SocketEntry(socket));
			} else {
				close = true;
			}
		}
		if (close) {
			if (shutdownHandler != null) {
				shutdownHandler.shutdown(socket);
			}
			if (!socket.isClosed()) {
				socket.getInputStream().close();
			}
			if (!socket.isClosed()) {
				socket.getOutputStream().close();
			}
			socket.close();
		}
	}
}
 
Example 3
Source Project: p4ic4idea   File: RpcSocketPool.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Release a socket back to the pool as no longer using
 * 
 * @param socket
 * @param shutdownHandler
 * @throws IOException
 */
public void release(Socket socket, ShutdownHandler shutdownHandler)
		throws IOException {
	if (isAlive(socket)) {
		boolean close = false;
		synchronized (this.pool) {
			if (this.pool.size() < size) {
				this.pool.add(new SocketEntry(socket));
			} else {
				close = true;
			}
		}
		if (close) {
			if (shutdownHandler != null) {
				shutdownHandler.shutdown(socket);
			}
			if (!socket.isClosed()) {
				socket.getInputStream().close();
			}
			if (!socket.isClosed()) {
				socket.getOutputStream().close();
			}
			socket.close();
		}
	}
}
 
Example 4
Source Project: p4ic4idea   File: RpcSocketPool.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Release a socket back to the pool as no longer using
 * 
 * @param socket
 * @param shutdownHandler
 * @throws IOException
 */
public void release(Socket socket, ShutdownHandler shutdownHandler)
		throws IOException {
	if (isAlive(socket)) {
		boolean close = false;
		synchronized (this.pool) {
			if (this.pool.size() < size) {
				this.pool.add(new SocketEntry(socket));
			} else {
				close = true;
			}
		}
		if (close) {
			if (shutdownHandler != null) {
				shutdownHandler.shutdown(socket);
			}
			if (!socket.isClosed()) {
				socket.getInputStream().close();
			}
			if (!socket.isClosed()) {
				socket.getOutputStream().close();
			}
			socket.close();
		}
	}
}
 
Example 5
Source Project: trufflesqueak   File: SqueakTCPSocket.java    License: MIT License 6 votes vote down vote up
private Status clientStatus() throws IOException {
    if (clientChannel == null) {
        return Status.Unconnected;
    }

    maybeCompleteConnection();
    final Socket socket = clientChannel.socket();

    if (socket.isInputShutdown()) {
        return Status.OtherEndClosed;
    }

    if (socket.isOutputShutdown()) {
        return Status.ThisEndClosed;
    }

    if (!socket.isConnected()) {
        return Status.Unconnected;
    }

    if (socket.isClosed()) {
        return Status.ThisEndClosed;
    }

    return Status.Connected;
}
 
Example 6
public synchronized void connect(final SocketFactory socketFactory, final int timeoutInMillis) throws Exception{
	mustNotBeConnected();
	
	if(socketFactory == null){
		throw new RuntimeException("NULL socket factory");
	}
	
	Socket socket = null;
	
	try{
		socket = socketFactory.createSocket();
		_connect(socket, timeoutInMillis);
	}catch(Exception e){
		if(socket != null){
			try{ if(!socket.isClosed()){ socket.close(); } }catch(Exception e0){}
		}
		throw e;
	}
}
 
Example 7
Source Project: Flink-CEPplus   File: BlobOutputStream.java    License: Apache License 2.0 5 votes vote down vote up
BlobOutputStream(JobID jobID, BlobKey.BlobType blobType, Socket socket) throws IOException {
	this.blobType = blobType;

	if (socket.isClosed()) {
		throw new IllegalStateException("BLOB Client is not connected. " +
			"Client has been shut down or encountered an error before.");
	}

	this.socket = socket;
	this.socketStream = socket.getOutputStream();
	this.md = BlobUtils.createMessageDigest();
	sendPutHeader(socketStream, jobID, blobType);
}
 
Example 8
public static final void closeSocket(final Socket pSocket) {
	if(pSocket != null && !pSocket.isClosed()) {
		try {
			pSocket.close();
		} catch (final IOException e) {
			Debug.e(e);
		}
	}
}
 
Example 9
@Override
protected void closeConnection() {
    for (IpNode receiverNode : receiverNodes.keySet()) {
        Socket socket = receiverNodes.get(receiverNode);
        if ((socket != null) && (!socket.isClosed())) {
            try {
                socket.close();
            } catch (IOException e) {
                logger.warn("Could not close socket {} in {}: {}", receiverNode, this.thing.getUID(),
                        e.getMessage());
            }
        }
        receiverNodes.put(receiverNode, null);
    }
}
 
Example 10
Source Project: p4ic4idea   File: RpcSocketPool.java    License: Apache License 2.0 5 votes vote down vote up
private void close(Socket socket) throws IOException {
	if (socket != null) {
		if (!socket.isClosed()) {
			socket.getInputStream().close();
		}
		if (!socket.isClosed()) {
			socket.getOutputStream().close();
		}
		socket.close();
	}
}
 
Example 11
/**
 * 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;
}
 
Example 12
Source Project: p4ic4idea   File: RpcSocketPool.java    License: Apache License 2.0 5 votes vote down vote up
private void close(Socket socket) throws IOException {
	if (socket != null) {
		if (!socket.isClosed()) {
			socket.getInputStream().close();
		}
		if (!socket.isClosed()) {
			socket.getOutputStream().close();
		}
		socket.close();
	}
}
 
Example 13
/** {@inheritDoc} */
@Override protected Socket openSocket(Socket sock, InetSocketAddress remAddr, IgniteSpiOperationTimeoutHelper timeoutHelper)
    throws IOException, IgniteSpiOperationTimeoutException {

    try {
        return super.openSocket(sock, remAddr, timeoutHelper);
    }
    catch (IgniteSpiOperationTimeoutException | IOException e) {
        if (sock.isClosed())
            sockets.remove(sock);

        throw e;
    }
}
 
Example 14
private void closeSocket(Socket socket) {
    try {
        if (!socket.isClosed()) {
            socket.close();
        }
    } catch (IOException e) {
        onError(new ProxyCacheException("Error closing socket", e));
    }
}
 
Example 15
private void closeSocket(Socket socket) {
    try {
        if (!socket.isClosed()) {
            socket.close();
        }
    } catch (IOException e) {
        onError(new ProxyCacheException("Error closing socket", e));
    }
}
 
Example 16
Source Project: p4ic4idea   File: RpcSocketPool.java    License: Apache License 2.0 5 votes vote down vote up
private void close(Socket socket) throws IOException {
	if (socket != null) {
		if (!socket.isClosed()) {
			socket.getInputStream().close();
		}
		if (!socket.isClosed()) {
			socket.getOutputStream().close();
		}
		socket.close();
	}
}
 
Example 17
private void closeSocket(Socket socket) {
    try {
        if (!socket.isClosed()) {
            socket.close();
        }
    } catch (IOException e) {
        //onError(new ProxyCacheException("Error closing socket", e));
    }
}
 
Example 18
Source Project: portforward   File: NetUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Quietly close a {@linkplain Socket} object. It tries to shutdown the input and output of the socket before close.
 * Exceptions are ignored.
 * 
 * @param socket
 *            the Socket object to close
 * @see #shutdown(Socket)
 */
public static void close(Socket socket) {
    if (socket == null) return;

    if (!socket.isClosed()) {
        shutdown(socket);
        try {
            socket.close();
        } catch (Throwable e) {
            log.debug(e.getMessage(), e);
        }
    }
}
 
Example 19
Source Project: G-Earth   File: ProxyProvider.java    License: MIT License 4 votes vote down vote up
protected void startProxyThread(Socket client, Socket server, HProxy proxy) throws IOException, InterruptedException {
    final boolean[] datastream = new boolean[1];
    server.setTcpNoDelay(true);
    client.setTcpNoDelay(true);

    client.setSoTimeout(0);
    server.setSoTimeout(0);

    if (HConnection.DEBUG) System.out.println(server.getLocalAddress().getHostAddress() + ": " + server.getLocalPort());
    Rc4Obtainer rc4Obtainer = new Rc4Obtainer(hConnection);

    OutgoingPacketHandler outgoingHandler = new OutgoingPacketHandler(server.getOutputStream(), hConnection.getTrafficObservables(), hConnection.getExtensionHandler());
    IncomingPacketHandler incomingHandler = new IncomingPacketHandler(client.getOutputStream(), hConnection.getTrafficObservables(), outgoingHandler, hConnection.getExtensionHandler());
    rc4Obtainer.setPacketHandlers(outgoingHandler, incomingHandler);

    Semaphore abort = new Semaphore(0);

    outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> {
        incomingHandler.setAsDataStream();
        proxy.verifyProxy(incomingHandler, outgoingHandler, hotelVersion);
        proxySetter.setProxy(proxy);
        datastream[0] = true;
        abortSemaphore = abort;
        onConnect();
    });

    handleInputStream(client, outgoingHandler, abort);
    handleInputStream(server, incomingHandler, abort);

    // abort can be acquired as soon as one of the sockets is closed
    abort.acquire();
    try	{
        if (!server.isClosed()) server.close();
        if (!client.isClosed()) client.close();
        if (HConnection.DEBUG) System.out.println("STOP");
        if (datastream[0]) {
            onConnectEnd();
        };
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 20
Source Project: gemfirexd-oss   File: FD_SOCK.java    License: Apache License 2.0 4 votes vote down vote up
@Override // GemStoneAddition  
public void run() {
  InputStream in;
    try {
        // GemStoneAddition - don't rely on exclusively on socket err to exit
        for (;;) {
          synchronized(mutex) {
            if(client_sock == null)
                break;
            in=client_sock.getInputStream();
          }
          int b = in.read();
          if (b == PROBE_TERMINATION) {
            synchronized(mutex) {
              client_sock.getOutputStream().write(PROBE_TERMINATION);
              client_sock.getOutputStream().flush();
              client_sock.shutdownOutput();
            }
          }
          if (b == ABNORMAL_TERMINATION || b == NORMAL_TERMINATION
              || b == PROBE_TERMINATION) {  // GemStoneAddition - health probes
            break;
          }
          if (Thread.currentThread().isInterrupted()) {
              break; // GemStoneAddition
          }
        } // for
    }
    catch(IOException io_ex1) {
    }
    finally {
        Socket sock = client_sock; // GemStoneAddition: volatile fetch
        if (sock != null && !sock.isClosed()) // GemStoneAddition
          closeClientSocket();
        // synchronized(clients} { clients.remove(this); }
        // GemStoneAddition rewrite avoiding iterators
        synchronized (myHandler.clientsMutex) {
          for (int i = 0; i < myHandler.clients.length; i ++) {
            if (myHandler.clients[i] == this) {
              myHandler.clients = (ClientConnectionHandler[])
                  Util.remove(myHandler.clients, i);
              break;
            }
          } // for
        } // synchronized
    }
}