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

The following examples show how to use java.net.Socket#isClosed() . 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 File: RpcSocketPool.java    From p4ic4idea with 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 File: RpcSocketPool.java    From p4ic4idea with 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 File: RemoteSPADEQueryConnection.java    From SPADE with GNU General Public License v3.0 6 votes vote down vote up
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 4
Source File: SqueakTCPSocket.java    From trufflesqueak with 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 5
Source File: RpcSocketPool.java    From p4ic4idea with 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 6
Source File: RpcSocketPool.java    From p4ic4idea with 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 7
Source File: HttpProxyCacheServer.java    From AndriodVideoCache with Apache License 2.0 5 votes vote down vote up
private void closeSocket(Socket socket) {
    try {
        if (!socket.isClosed()) {
            socket.close();
        }
    } catch (IOException e) {
        onError(new ProxyCacheException("Error closing socket", e));
    }
}
 
Example 8
Source File: NetUtils.java    From portforward with 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 9
Source File: HttpProxyCacheServer.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
private void closeSocket(Socket socket) {
    try {
        if (!socket.isClosed()) {
            socket.close();
        }
    } catch (IOException e) {
        //onError(new ProxyCacheException("Error closing socket", e));
    }
}
 
Example 10
Source File: RpcSocketPool.java    From p4ic4idea with 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
Source File: HttpProxyCacheServer.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
private void closeSocket(Socket socket) {
    try {
        if (!socket.isClosed()) {
            socket.close();
        }
    } catch (IOException e) {
        onError(new ProxyCacheException("Error closing socket", e));
    }
}
 
Example 12
Source File: BlobOutputStream.java    From Flink-CEPplus with 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 13
Source File: TcpClientDiscoveryUnresolvedHostTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@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
Source File: RpcSocketPool.java    From p4ic4idea with 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 15
Source File: SSLSocketFactory.java    From Popeens-DSub with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 16
Source File: RpcSocketPool.java    From p4ic4idea with 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
Source File: Lib485BridgeHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@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 18
Source File: SocketUtils.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
public static final void closeSocket(final Socket pSocket) {
	if(pSocket != null && !pSocket.isClosed()) {
		try {
			pSocket.close();
		} catch (final IOException e) {
			Debug.e(e);
		}
	}
}
 
Example 19
Source File: ProxyProvider.java    From G-Earth with 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 File: FD_SOCK.java    From gemfirexd-oss with 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
    }
}