Example usage for java.net Socket close

List of usage examples for java.net Socket close

Introduction

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

Prototype

public synchronized void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:org.jasig.portlet.cms.model.security.ClamAVAntiVirus.java

private void closeSocket(final Socket socket) {
    try {/*from   w  ww . j  ava 2s . c om*/
        if (socket != null)
            socket.close();
    } catch (final IOException e) {

    }
}

From source file:fr.treeptik.cloudunit.utils.PortUtils.java

private boolean isPortOpened(String ip, Integer port) {
    try {/*from  w w  w. j  av  a 2s  .c  om*/
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(ip, port), 500);
        socket.close();
        return true;
    } catch (Exception ex) {
        return false;
    }
}

From source file:neohope.mule.hl7v2.socket.AbstractTcpSocketFactory.java

public void passivateObject(Object key, Object object) throws Exception {
    Hl7v2SocketKey socketKey = (Hl7v2SocketKey) key;

    boolean keepSocketOpen = MapUtils.getBooleanValue(socketKey.getEndpoint().getProperties(),
            Hl7v2Connector.KEEP_SEND_SOCKET_OPEN_PROPERTY, socketKey.getConnector().isKeepSendSocketOpen());
    Socket socket = (Socket) object;

    if (!keepSocketOpen) {
        try {//from   w  w w  . ja v a 2  s.c  om
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            logger.debug("Failed to close socket after dispatch: " + e.getMessage());
        }
    }
}

From source file:com.edmunds.etm.agent.impl.TcpHealthCheck.java

protected boolean connectToServer() {

    InetAddress addr = getHostAddress();

    boolean connected;
    try {//  w  w w  .java 2s  .com
        Socket socket = new Socket(addr, port);
        socket.close();
        connected = true;
    } catch (IOException e) {
        connected = false;
    }

    if (logger.isDebugEnabled()) {
        String message = String.format("Connected to server with result: %b", connected);
        logger.debug(message);
    }

    return connected;
}

From source file:com.predic8.membrane.core.transport.http.HttpEndpointListener.java

/**
 * Closes all connections or those that are <i>currently</i> idle.
 * @param onlyIdle whether to close only idle connections
 * @return true, if there are no more open connections
 *//*from  w  ww .  j  ava2  s  .  c o m*/
public boolean closeConnections(boolean onlyIdle) throws IOException {
    if (!closed)
        throw new IllegalStateException("please call closePort() fist.");
    for (Socket s : (onlyIdle ? idleSockets : openSockets).keySet())
        if (!s.isClosed())
            s.close();
    return openSockets.isEmpty();
}

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testRequest() throws Exception {
    byte[] dataToWrite = new byte[] { 5, 4, 3, 2 };
    expectCall().andReturn(emptyResponse());

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    IOUtils.write(dataToWrite, socket.getOutputStream());
    socket.close();
    assertThat(recordedConnections().get(0), data(is(dataToWrite)));
    assertThat(recordedConnections().get(0), address(is("example.org:1234")));
}

From source file:com.predic8.membrane.core.transport.http.HttpEndpointListener.java

void setIdleStatus(Socket socket, boolean isIdle) throws IOException {
    if (isIdle) {
        if (closed) {
            socket.close();
            throw new SocketException();
        }//from ww  w . ja  v  a  2s  .c o m
        idleSockets.put(socket, Boolean.TRUE);
    } else {
        idleSockets.remove(socket);
    }
}

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testResponse() throws Exception {
    byte[] mockData = new byte[] { 1, 2, 3, 4 };
    expectCall().andReturn(data(mockData));

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    byte[] data = IOUtils.toByteArray(socket.getInputStream());
    socket.close();
    assertThat(data, is(mockData));/*from   ww  w.  ja v  a2  s.co  m*/
}

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testConditionalAddress() throws Exception {
    byte[] mockData = new byte[] { 1, 2, 3, 4 };
    expectCall().andWhenRequest(address(is("example.org:1234"))).thenReturn(data(mockData));

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    byte[] data = IOUtils.toByteArray(socket.getInputStream());
    socket.close();
    assertThat(data, is(mockData));//from   w ww . ja  v  a 2s. c  o  m
}

From source file:greensopinion.restexample.test.web.WebApplicationContainer.java

private boolean testForSuccessfulStartup() {
    // test to see if the web container is listening on the address/port combo
    try {//from  w  w  w . j  a  v a 2  s . c om
        Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
        socket.close();
        return true;
    } catch (UnknownHostException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        return false;
    }
}