Example usage for java.net Socket isConnected

List of usage examples for java.net Socket isConnected

Introduction

In this page you can find the example usage for java.net Socket isConnected.

Prototype

public boolean isConnected() 

Source Link

Document

Returns the connection state of the socket.

Usage

From source file:org.csploit.android.core.System.java

public static boolean isPortAvailable(int port) {
    boolean available = true;

    int available_code = mOpenPorts.get(port);

    if (available_code != 0)
        return available_code != 1;

    try {/*ww w  .jav  a2  s .  co m*/
        // attempt 3 times since proxy and server could be still releasing
        // their ports
        for (int i = 0; i < 3; i++) {
            Socket channel = new Socket();
            InetSocketAddress address = new InetSocketAddress(
                    InetAddress.getByName(mNetwork.getLocalAddressAsString()), port);

            channel.connect(address, 200);

            available = !channel.isConnected();

            channel.close();

            if (available)
                break;

            Thread.sleep(200);
        }
    } catch (Exception e) {
        available = true;
    }

    mOpenPorts.put(port, available ? 2 : 1);

    return available;
}

From source file:com.googlecode.jmxtrans.connections.SocketFactory.java

/**
 * Validates that the socket is good./*from   w w w  . j a va  2s. c o  m*/
 */
@Override
public boolean validateObject(InetSocketAddress address, Socket socket) {
    if (socket == null) {
        log.error("Socket is null [{}]", address);
        return false;
    }

    if (!socket.isBound()) {
        log.error("Socket is not bound [{}]", address);
        return false;
    }
    if (socket.isClosed()) {
        log.error("Socket is closed [{}]", address);
        return false;
    }
    if (!socket.isConnected()) {
        log.error("Socket is not connected [{}]", address);
        return false;
    }
    if (socket.isInputShutdown()) {
        log.error("Socket input is shutdown [{}]", address);
        return false;
    }
    if (socket.isOutputShutdown()) {
        log.error("Socket output is shutdown [{}]", address);
        return false;
    }
    return true;
}

From source file:org.wso2.carbon.connector.ISO8583.ISO8583MessageHandler.java

/**
 * handle the iso8583 message request and responses
 *
 * @param isoMessage  packed ISOMessage/*from  w ww . j a  v  a2  s  .com*/
 * @param connection  Socket connection with backend Test server
 * @param messageContext the message context
 */
public void clientHandler(MessageContext messageContext, Socket connection, String isoMessage) {
    DataOutputStream outStream = null;
    BufferedReader inFromServer = null;
    String message;
    try {
        outStream = new DataOutputStream(connection.getOutputStream());
        inFromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        if (connection.isConnected()) {
            outStream.writeUTF(isoMessage);
            outStream.flush();

            /* Sender will receive the Acknowledgement here */
            if ((message = inFromServer.readLine()) != null) {
                unpackResponse(messageContext, message);
            }
        }
    } catch (IOException e) {
        handleException("An exception occurred in sending the ISO8583 message", e);
    } finally {
        try {
            if (outStream != null) {
                outStream.close();
            }
            if (inFromServer != null) {
                inFromServer.close();
            }
            connection.close();
        } catch (IOException e) {
            log.error("Couldn't close the I/O Streams", e);
        }
    }
}

From source file:org.sakaiproject.antivirus.impl.ClamAVScanner.java

protected Socket getClamdSocket() throws UnknownHostException {

    InetAddress address = InetAddress.getByName(getHost());
    Socket socket = null;
    try {/*from   w ww  .j a  va  2 s  .  c o  m*/
        socket = new Socket();
        SocketAddress socketAddress = new InetSocketAddress(address, getPort());
        socket.connect(socketAddress, 5000);
        if (!socket.isConnected()) {
            return null;
        }
        return socket;
    } catch (IOException ioe) {
        logger.error("Exception caught acquiring main socket to CLAMD on " + address + " on port " + getPort()
                + ": " + ioe.getMessage());
    }
    return socket;
}

From source file:com.mindprotectionkit.freephone.signaling.SignalingSocket.java

private Socket timeoutHackConnect(SSLSocketFactory sslSocketFactory, String host, int port) throws IOException {
    InetAddress[] addresses = InetAddress.getAllByName(host);
    Socket stagedSocket = LowLatencySocketConnector.connect(addresses, port);

    Log.w("SignalingSocket", "Connected to: " + stagedSocket.getInetAddress().getHostAddress());

    SocketConnectMonitor monitor = new SocketConnectMonitor(stagedSocket);

    monitor.start();//from   w w w.j  a  va  2  s.c  om

    Socket result = sslSocketFactory.createSocket(stagedSocket, host, port, true);

    synchronized (this) {
        this.connectionAttemptComplete = true;
        notify();

        if (result.isConnected())
            return result;
        else
            throw new IOException("Socket timed out before " + "connection completed.");
    }
}

From source file:com.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testConnectSuccess() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.expect(sockMock.isConnected()).andReturn(true);
    EasyMock.replay(sockMock);//from ww  w. j av a 2s. co m

    boolean started = shoutcast.start(sockMock);

    Assert.assertTrue(started);
    Assert.assertTrue(shoutcast.isStarted());
}

From source file:com.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testUpdateMetadata() throws IOException, URISyntaxException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.expect(sockMock.isConnected()).andReturn(true);
    EasyMock.replay(sockMock);//from ww  w  .j a v a  2s  .  c o  m

    shoutcast.start(sockMock);

    HttpUriRequest request = shoutcast.getUpdateMetadataRequest("song", "artist", "album");
    Assert.assertEquals(shoutcast.getHost(), request.getURI().getHost());
    Assert.assertEquals(shoutcast.getPort(), request.getURI().getPort());

    HashMap<String, String> paramsMap = getParams(request);
    Assert.assertEquals("album song artist", paramsMap.get("song"));
}

From source file:com.supernovapps.audio.jstreamsourcer.IcecastTest.java

@Test
public void testConnectSuccess() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.expect(sockMock.isConnected()).andReturn(true);
    EasyMock.replay(sockMock);/*from  w  ww  .j ava 2s  . co m*/

    boolean started = icecast.start(sockMock);

    Assert.assertTrue(started);
    Assert.assertTrue(icecast.isStarted());
}

From source file:com.supernovapps.audio.jstreamsourcer.IcecastTest.java

@Test
public void testUpdateMetadata() throws IOException, URISyntaxException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.expect(sockMock.isConnected()).andReturn(true);
    EasyMock.replay(sockMock);//w w w  . j  a  v  a2s  .  c o m

    icecast.start(sockMock);

    HttpUriRequest request = icecast.getUpdateMetadataRequest("song", "artist", "album");
    Assert.assertEquals(icecast.getHost(), request.getURI().getHost());
    Assert.assertEquals(icecast.getPort(), request.getURI().getPort());

    HashMap<String, String> paramsMap = getParams(request);
    Assert.assertEquals(icecast.getPath(), paramsMap.get("mount"));
    Assert.assertEquals("updinfo", paramsMap.get("mode"));
    Assert.assertEquals("album song artist", paramsMap.get("song"));
}

From source file:ca.farrelltonsolar.classic.PVOutputUploader.java

private Socket Connect(String hostname) throws InterruptedException {
    final Socket mySocket = new Socket();
    do {//  ww w . jav  a2  s .  c om
        try {
            InetAddress ipaddress = InetAddress.getByName(hostname);
            SocketAddress address = new InetSocketAddress(ipaddress, 80);
            mySocket.connect(address, 3500);
        } catch (IOException ex) {
            Log.w(getClass().getName(),
                    String.format("PVOutput trying to connect to %s, failed ex: %s", hostname, ex));
            Thread.sleep(2 * 60000);
        }
    } while (mySocket.isConnected() == false);
    return mySocket;
}