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:com.linkedin.d2.quorum.ZKPeer.java

/**
 * Send the 4letterword//from   www.  j ava 2s .c o m
 *
 * @param host- destination host
 * @param port- destination port
 * @param cmd- the 4letterword (stat, srvr,etc. - see
 *          http://zookeeper.apache.org/doc/r3.3.3/zookeeperAdmin.html#sc_zkCommands)
 * @return
 * @throws IOException
 */
public static String sendCommand(String host, int port, String cmd) throws IOException {
    // NOTE: ignore CancelledKeyException in logs caused by
    // https://issues.apache.org/jira/browse/ZOOKEEPER-1237
    Socket sock = new Socket(host, port);
    BufferedReader reader = null;
    try {
        OutputStream outstream = sock.getOutputStream();
        outstream.write(cmd.getBytes());
        outstream.flush();
        sock.shutdownOutput();

        reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        return sb.toString();
    } finally {
        sock.close();
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.smartwork.client.gsocket.CommonSocketFactory.java

public void destroyObject(Object key, Object obj) throws Exception {
    Socket socket = (Socket) obj;
    socket.close();
}

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

/**
 * Closes the socket.// ww w  . j a  v  a 2  s .  c  om
 */
@Override
public void destroyObject(InetSocketAddress address, Socket socket) throws Exception {
    socket.close();
}

From source file:com.mirth.connect.connectors.file.filesystems.HaltableFTPClient.java

public void closeDataSockets() {
    // Close all open sockets
    synchronized (openSockets) {
        for (Iterator<Socket> iterator = openSockets.iterator(); iterator.hasNext();) {
            Socket socket = iterator.next();
            try {
                socket.close();
            } catch (IOException e) {
            } finally {
                iterator.remove();//from ww w.  j  a v  a 2  s  .c  o  m
            }
        }
    }
}

From source file:Main.java

public void run() {
    while (true) {
        try {//from ww  w  .  ja v  a 2 s .  com
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
            Socket client = serverSocket.accept();

            System.out.println("Just connected to " + client.getRemoteSocketAddress());
            client.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

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

public void destroyObject(Object key, Object object) throws Exception {
    Socket socket = (Socket) object;
    if (!socket.isClosed()) {
        socket.close();
    }//from   w  ww .  ja  v a 2s .  c  om
}

From source file:marytts.util.io.FileUtils.java

public static void close(Socket socket, Closeable... closeables) {
    for (Closeable c : closeables) {
        if (c != null) {
            try {
                c.close();//from  w ww.j  a  v  a 2  s.  co m
            } catch (Exception ex) {
                MaryUtils.getLogger(FileUtils.class.getName()).log(Level.WARN, "Couldn't close Closeable.", ex);
            }
        }
    }
    if (socket != null) {
        try {
            socket.close();
        } catch (Exception ex) {
            MaryUtils.getLogger(FileUtils.class.getName()).log(Level.WARN, "Couldn't close Socket.", ex);
        }
    }
}

From source file:com.nokia.dempsy.messagetransport.tcp.TcpSender.java

protected void closeQuietly(Socket socket) {
    if (socket != null) {
        try {//w w w  . ja  v a2s.  c om
            socket.close();
        } catch (IOException ioe) {
            if (logger.isDebugEnabled())
                logger.debug("close socket failed for " + destination);
        }
    }
}

From source file:net.oauth.client.httpclient4.OAuthSchemeTest.java

@Override
public void setUp() throws Exception {
    { // Get an ephemeral local port number:
        Socket s = new Socket();
        s.bind(null);//ww w . j  a v  a 2 s  . c o  m
        port = s.getLocalPort();
        s.close();
    }
    server = new Server(port);
    Context servletContext = new Context(server, "/", Context.SESSIONS);
    servletContext.addServlet(new ServletHolder(new ProtectedResource()), "/Resource/*");
    server.start();
    context = new BasicHttpContext();
    context.setAttribute(ClientContext.AUTH_SCHEME_PREF, Arrays.asList(OAuthSchemeFactory.SCHEME_NAME));
    client = new DefaultHttpClient();
    client.getAuthSchemes().register(OAuthSchemeFactory.SCHEME_NAME, new OAuthSchemeFactory());
    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", port),
            new OAuthCredentials(ProtectedResource.ACCESSOR));
}

From source file:com.symbian.utils.cmdline.argscheckers.OpenSocketData.java

/** {@inheritDoc}
 * @see com.symbian.utils.cmdline.argscheckers.DataAcceptable#check(java.lang.String)
 * @param aString {@inheritDoc}// w w w  . jav a 2  s  .co m
 * @throws ParseException {@inheritDoc}
 */
public void check(final String aString) throws ParseException {
    String[] lSplitSocket = aString.split(":");
    if (lSplitSocket.length < 1 || lSplitSocket.length > 2) {
        throw new ParseException("illegal socket name");
    }

    int lPort = (lSplitSocket.length == 1) ? DEFAULT_PORT : Integer.parseInt(lSplitSocket[1]);
    if (lPort == 0) {
        throw new ParseException("illegal port number");
    }

    String lServer = lSplitSocket[0];

    Socket lSocket = null;

    // connect to server
    try {
        lSocket = new Socket(lServer, lPort);
        lSocket.close();
        lSocket = null;
    } catch (Exception lException) {
        throw new ParseException("cannot connect to socket '" + aString + "'");
    }
}