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.transdroid.daemon.util.TlsSniSocketFactory.java

@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public Socket createSocket(Socket plainSocket, String host, int port, boolean autoClose) throws IOException {
    if (autoClose) {
        // we don't need the plainSocket
        plainSocket.close();
    }/*from  w  w  w . ja v a  2  s . c o m*/

    SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory
            .getDefault(0);

    // For self-signed certificates use a custom trust manager
    if (acceptAllCertificates) {
        sslSocketFactory.setTrustManagers(new TrustManager[] { new IgnoreSSLTrustManager() });
    } else if (selfSignedCertificateKey != null) {
        sslSocketFactory
                .setTrustManagers(new TrustManager[] { new SelfSignedTrustManager(selfSignedCertificateKey) });
    }

    // create and connect SSL socket, but don't do hostname/certificate verification yet
    SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);

    // enable TLSv1.1/1.2 if available
    ssl.setEnabledProtocols(ssl.getSupportedProtocols());

    // set up SNI before the handshake
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        sslSocketFactory.setHostname(ssl, host);
    } else {
        try {
            java.lang.reflect.Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class);
            setHostnameMethod.invoke(ssl, host);
        } catch (Exception e) {
            Log.d(TlsSniSocketFactory.class.getSimpleName(), "SNI not usable: " + e);
        }
    }

    // verify hostname and certificate
    SSLSession session = ssl.getSession();
    if (!(acceptAllCertificates || selfSignedCertificateKey != null)
            && !hostnameVerifier.verify(host, session)) {
        throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
    }

    /*DLog.d(TlsSniSocketFactory.class.getSimpleName(),
    "Established " + session.getProtocol() + " connection with " + session.getPeerHost() +
          " using " + session.getCipherSuite());*/

    return ssl;
}

From source file:it.evilsocket.dsploit.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 ? false : true;

    try {//w  w  w  .ja va 2 s.c  om
        // 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:de.iew.imageupload.actions.UploadImageAction.java

public void actionPerformed(ActionEvent ev) {
    try {//w  ww .ja v a 2  s.co  m
        Socket socket = new Socket("squeeze", 8124);

        FileInputStream fin = new FileInputStream(this.fileToUpload);
        OutputStream out = socket.getOutputStream();

        IOUtils.copy(fin, out);

        out.flush();
        fin.close();
        out.close();

        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.l2jfree.gameserver.geoeditorcon.GeoEditorListener.java

@Override
public void run() {
    Socket connection = null;
    try {/*from  w ww. jav  a2 s.co m*/
        while (true) {
            connection = _serverSocket.accept();
            if (_geoEditor != null && _geoEditor.isWorking()) {
                _log.warn("Geoeditor already connected!");
                connection.close();
                continue;
            }
            _log.info("Received geoeditor connection from: " + connection.getInetAddress().getHostAddress());
            _geoEditor = new GeoEditorThread(connection);
            _geoEditor.start();
        }
    } catch (Exception e) {
        _log.info("GeoEditorListener: ", e);
        try {
            if (connection != null)
                connection.close();
        } catch (Exception e2) {
        }
    } finally {
        try {
            _serverSocket.close();
        } catch (IOException io) {
            _log.info("", io);
        }
        _log.warn("GeoEditorListener Closed!");
    }
}

From source file:delete.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w  w w . jav  a2s.co m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    Socket s = new Socket("localhost", 9999);

    DataInputStream in = new DataInputStream(s.getInputStream());
    DataOutputStream outD = new DataOutputStream(s.getOutputStream());

    outD.writeUTF(request.getParameter("employee_id"));

    in.readUTF();

    s.close();

    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */

        out.println("Deleted");
    }
}

From source file:com.tasktop.c2c.server.web.proxy.ajp.AjpPoolableConnectionFactory.java

@Override
public Object makeObject(Object objectKey) throws Exception {
    Key key = (Key) objectKey;
    String host = key.getHost();/* w  w w. j ava  2  s .  c o m*/
    int port = key.getPort();
    if (port <= 0) {
        port = 8009;
    }

    Socket socket = socketFactory.createSocket(host, port);
    try {
        socket.setTcpNoDelay(tcpNoDelay);
        socket.setSoTimeout(soTimeout);
        socket.setKeepAlive(keepAlive);
    } catch (SocketException e) {
        socket.close();
        throw e;
    }
    debug("Created new socket: " + socket.toString());
    return socket;
}

From source file:org.mule.module.http.functional.listener.HttpListenerPersistentConnectionsTestCase.java

protected void assertConnectionClosesAfterSend(DynamicPort port, HttpVersion httpVersion) throws IOException {
    Socket socket = new Socket("localhost", port.getNumber());
    sendRequest(socket, httpVersion);/*from  ww w .ja  va 2s  .  c  om*/
    assertResponse(getResponse(socket), true);

    sendRequest(socket, httpVersion);
    assertResponse(getResponse(socket), false);

    socket.close();
}

From source file:org.jboss.pnc.environment.docker.DockerEnvironmentDriverRemoteTest.java

private boolean testOpenedPort(final int port) {
    try {/*from   w  ww.j a  v a2  s .c  om*/
        final Socket echoSocket = new Socket(dockerIp, port);
        echoSocket.close();
    } catch (final IOException e) {
        return false;
    }
    return true;
}

From source file:org.apache.chemistry.opencmis.client.bindings.spi.http.AbstractApacheClientHttpInvoker.java

/**
 * Closes the given socket and ignores exceptions.
 *//*from  www. j a  v a 2 s  .  c  o m*/
protected void closeSocket(Socket socket) {
    try {
        socket.close();
    } catch (IOException ioe) {
        // ignore
    }
}

From source file:ezbake.thriftrunner.starters.SimpleStarter.java

boolean clientSideSocketIsFree(int port) {
    Socket clientSocket = null;
    try {/*from w ww . j  ava 2  s .  c o  m*/
        clientSocket = new Socket(hostName, port);
    } catch (final IOException e) {
        return true;
    } finally {
        if (clientSocket != null) {
            try {
                clientSocket.close();
            } catch (final IOException e) {
                // Should never happen
            }
        }
    }
    return false;
}