Socket isConnected - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Socket isConnected method.

This code example shows how to use the following methods:isConnected
   */ 
  public boolean isOpen() { 
    if (socket == null) {
      return false; 
    } 
    return socket.isConnected();
  } 
 
  /** 
   * Connects the socket, creating a new socket object if necessary. 
   */ 
  public void open() throws TTransportException { 
    if (isOpen()) { 
      throw new TTransportException(TTransportException.ALREADY_OPEN, "Socket already connected."); 
    } 
 
    if (host.length() == 0) {
      throw new TTransportException(TTransportException.NOT_OPEN, "Cannot open null host."); 
    } 
    if (port <= 0) {
8
CodeRank
This code example shows how to use the following methods:close, isConnected, setReuseAddress, setSoLinger
    public static boolean verifyConnection(String host, int port)
    { 
        Socket socket = null;
        try 
        { 
            socket = new Socket(host, port);
            socket.setReuseAddress(true);
            socket.setSoLinger(true, 0);
            boolean isConnected = socket.isConnected();
            return isConnected;
        } 
        catch (UnknownHostException e)
        { 
            logger.warn("{}:{} is still down", host, port);
            return false; 
        } 
        catch (IOException e)
        { 
            logger.warn("{}:{} is still down", host, port);
            return false; 

7
CodeRank
This code example shows how to use the following methods:isConnected
        } 
    } 
 
    public boolean isConnected() 
    { 
        return socket.isConnected();
    } 
 
    public InetAddress getLocalAddress()
    { 
        return socket.getLocalAddress();
    } 
 
    public int getLocalPort() 
    { 
        return socket.getLocalPort();
    } 
 
    public InetAddress getRemoteAddress()
    { 
7
CodeRank
This code example shows how to use the following methods:close, connect, isConnected
public class Knock { 
	public static boolean doTCPKnock (InetAddress host, int port, int timeout) {
		Socket s;
		try { 
			// s = new Socket(host, port); 
			s = new Socket();
			InetSocketAddress socket = new InetSocketAddress(host, port);
			// Use user-provided timeout 
			s.connect(socket, timeout);
			if (s.isConnected())
				s.close();
		} catch (UnknownHostException ex) {
			return false; 
		} catch (IllegalArgumentException ex) {
			return false; 
		} catch (ConnectException ex) {
			// We dismiss "connection refused" as knockd operates at link-layer 
		} catch (SocketTimeoutException ex) {
			// Ignore timeout -- went through all the above junk to send the 
			// packets within 15 seconds 
6
CodeRank
This code example shows how to use the following methods:isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown
    public void disconnect() { 
        if (isConnected()) { 
            try { 
                inputStream.close();
                outputStream.close();
                if (!socket.isClosed()) {
                    socket.close();
                } 
            } catch (IOException ex) {
                throw new JedisConnectionException(ex);
            } 
        } 
    } 
 
    public boolean isConnected() { 
        return socket != null && socket.isBound() && !socket.isClosed()
                && socket.isConnected() && !socket.isInputShutdown()
                && !socket.isOutputShutdown();
    } 
 

9
CodeRank
This code example shows how to use the following methods:isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown
     * Returns true if the Socket is still alive. 
     * 
     * @return true if the Socket is still alive 
     */ 
    private boolean isSocketLive() { 
        return (socket.isBound() &&
                !socket.isClosed() && socket.isConnected() &&
                !socket.isInputShutdown() && !socket.isOutputShutdown());
    } 
 
 
    /** 
     * Read a line of text. A line is considered to be terminated  
     * by any one of a line feed ('\n'), a carriage return ('\r'), 
     * or a carriage return followed immediately by a linefeed.  
     * 
     * @return A String containing the contents of the line,  
     * not including any line-termination 
     * characters, or null if the end of the stream has been reached 
     * 
7
CodeRank
This code example shows how to use the following methods:getInputStream, getOutputStream, isConnected
			return false; 
		} 
		 
		 
		// try opening socket even if it's not null. 
		if (socket.isConnected() != true) {
			openSocket(); 
		} 
 
		// Is the socket open? 
		if (socket.isConnected() != true) {
			if (DEBUG) msg("After opening socket, it was not connected.");
			return false; 
		} 
 
		if (DEBUG) msg("Successfully opened socket.");
		return true; 
	} 
 
	/** 

6
CodeRank
This code example shows how to use the following methods:close, connect, isConnected
    } 
 
    public String call() {
        boolean result = false;
 
        Socket socket = new Socket();
 
        try { 
            socket.connect(new InetSocketAddress(address, port), TIMEOUT);
            result = socket.isConnected();
            socket.close();
          } catch (Exception e) {
          } 
 
        return (result) ? address + ":" + port : null;
    } 
 
} 
6
CodeRank
This code example shows how to use the following methods:isClosed, isConnected
    } 
 
    @Override 
    public boolean isOpen() { 
        return (mIn != null && mOut != null &&
                mSocket != null && mSocket.isConnected() && !mSocket.isClosed());
    } 
 
    /** 
     * Close the connection.  MUST NOT return any exceptions - must be "best effort" and safe. 
     */ 
    @Override 
    public void close() { 
        try { 
            mIn.close();
        } catch (Exception e) {
            // May fail if the connection is already closed. 
        } 
        try { 
            mOut.close();
6
CodeRank
This code example shows how to use the following methods:isConnected
	    	 
	    } 
	     
	    public boolean connectionstatus(){ 
	    	if(client != null){
	    		return client.isConnected()&&connectionstatus;
	    	} 
	    	return false; 
	    } 
	     
	    public void disconnect(){ 
	    	connectionstatus=false;
	    		try { 
	    			if(in != null)
	    				in.close();				
	    			if(out != null)
	    				out.close();
	    			if(client!=null)
	    				client.close();
	    			// TODO set send to pc key to not connected state 
6
CodeRank

Related Socket isConnected Questions & Answers:

isConnected Method Overview
Returns the connection state of the socket.
See Code Examples for other Java 8 Socket Methods: