Example usage for java.net Socket getPort

List of usage examples for java.net Socket getPort

Introduction

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

Prototype

public int getPort() 

Source Link

Document

Returns the remote port number to which this socket is connected.

Usage

From source file:org.openadaptor.auxil.connector.socket.SocketReadConnector.java

private static String getConnectionName(Socket s) {
    return s.getInetAddress().getHostAddress() + ":" + s.getPort();
}

From source file:Messenger.TorLib.java

public static void postToURL(String hostname, int port, String postKey, String data) throws IOException {
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);/*from   w  ww .  j  a  va  2  s  .  c om*/
    sslSocket.startHandshake();
    String path = "/" + postKey;
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");

    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
    sslSocket.close();
}

From source file:SocketFetcher.java

/**
 * Start TLS on an existing socket. Supports the "STARTTLS" command in many
 * protocols.//  w w  w.j  a v a 2  s  . c  o m
 */
public static Socket startTLS(Socket socket, Properties props, String prefix) throws IOException {
    InetAddress a = socket.getInetAddress();
    String host = a.getHostName();
    int port = socket.getPort();
    // System.out.println("SocketFetcher: startTLS host " + host + ", port " +
    // port);

    try {
        SSLSocketFactory ssf;
        String sfClass = props.getProperty(prefix + ".socketFactory.class", null);
        SocketFactory sf = getSocketFactory(sfClass);
        if (sf != null && sf instanceof SSLSocketFactory)
            ssf = (SSLSocketFactory) sf;
        else
            ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
        socket = ssf.createSocket(socket, host, port, true);
        configureSSLSocket(socket, props, prefix);
    } catch (Exception ex) {
        if (ex instanceof InvocationTargetException) {
            Throwable t = ((InvocationTargetException) ex).getTargetException();
            if (t instanceof Exception)
                ex = (Exception) t;
        }
        if (ex instanceof IOException)
            throw (IOException) ex;
        // wrap anything else before sending it on
        IOException ioex = new IOException(
                "Exception in startTLS: host " + host + ", port " + port + "; Exception: " + ex);
        throw ioex;
    }
    return socket;
}

From source file:Messenger.TorLib.java

/**
 * This method makes a http GET request for the specified resource to the specified hostname.
 * It uses the SOCKS proxy to a connection over Tor.
 * The DNS lookup is also done over Tor.
 * This method only uses port 443 for SSL.
 *
 * @param hostname hostname for target server.
 * @param port port to connect to./*from  w w  w  .  j a v  a2s  .  com*/
 * @param resource resource to lookup with GET request.
 * @return returns a JSON object.
 * @throws IOException
 * @throws JSONException
 */
public static JSONObject getJSON(String hostname, int port, String resource)
        throws IOException, JSONException, HttpException {
    //Create a SSL socket using Tor
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);
    sslSocket.startHandshake();
    openSockets.add(sslSocket);

    //Create the HTTP GET request and push it over the outputstream
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("GET /" + resource + " HTTP/1.0\r\n");
    wr.write("Host: " + hostname + "\r\n");
    wr.write("\r\n");
    wr.flush();

    //Listen for a response on the inputstream
    BufferedReader br = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String t;
    boolean start = false;
    String output = "";
    while ((t = br.readLine()) != null) {
        if (t.equals("")) {
            start = true;
        }
        if (start) {
            output = output + t;
        }
    }
    br.close();
    wr.close();
    sslSocket.close();
    System.out.println(output);
    openSockets.remove(sslSocket);
    return new JSONObject(output);
}

From source file:SocketFetcher.java

/**
 * Start TLS on an existing socket.//from ww  w .  ja va2  s.c o m
 * Supports the "STARTTLS" command in many protocols.
 */
public static Socket startTLS(Socket socket, Properties props, String prefix) throws IOException {
    InetAddress a = socket.getInetAddress();
    String host = a.getHostName();
    int port = socket.getPort();
    //System.out.println("SocketFetcher: startTLS host " + host + ", port " + port);

    try {
        SSLSocketFactory ssf;
        String sfClass = props.getProperty(prefix + ".socketFactory.class", null);
        SocketFactory sf = getSocketFactory(sfClass);
        if (sf != null && sf instanceof SSLSocketFactory)
            ssf = (SSLSocketFactory) sf;
        else
            ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
        socket = ssf.createSocket(socket, host, port, true);
        configureSSLSocket(socket, props, prefix);
    } catch (Exception ex) {
        if (ex instanceof InvocationTargetException) {
            Throwable t = ((InvocationTargetException) ex).getTargetException();
            if (t instanceof Exception)
                ex = (Exception) t;
        }
        if (ex instanceof IOException)
            throw (IOException) ex;
        // wrap anything else before sending it on
        IOException ioex = new IOException(
                "Exception in startTLS: host " + host + ", port " + port + "; Exception: " + ex);
        ioex.initCause(ex);
        throw ioex;
    }
    return socket;
}

From source file:de.fun2code.google.cloudprint.push.PushReceiver.java

/**
 * Sets up the SSL socket for use with XMPP.
 * /*from w  ww. j  av  a  2 s.co m*/
 * @param socket
 *            the socket to do TLS over.
 * @return   The SSL Socket.
 * @throws IOException
 */
public static SSLSocket setupSSLSocket(Socket socket) throws NoSuchAlgorithmException, KeyManagementException,
        KeyStoreException, UnrecoverableKeyException, IOException {
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    TrustManagerFactory tfactory = TrustManagerFactory.getInstance("SunPKIX");
    tfactory.init(keyStore);
    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, socket.getInetAddress().getHostAddress(),
            socket.getPort(), true);
    sslsocket.setUseClientMode(true);
    return sslsocket;
}

From source file:org.apache.hadoop.thriftfs.ThriftServerContext.java

/**
 * Tries to get the client name for a given Thrift transport.
 * //from www . ja  va  2  s .c o  m
 * @param t the Thrift transport
 * @return The client name, if 'transport' was a socket, or
 *         "unknown-client:<random number>" otherwise.
 */
private String assignClientName() {
    Socket sock = getTransportSocket();
    if (sock != null) {
        return sock.getInetAddress().getHostAddress() + ":" + sock.getPort();
    }
    return "unknown-client:" + random.nextLong();
}

From source file:com.googlecode.android_scripting.facade.EventServer.java

@Override
protected void handleConnection(Socket socket) throws IOException {
    Listener l = new Listener(socket);
    Log.v("Adding EventServer listener " + socket.getPort());
    mListeners.add(l);/*from  w  w w.  j av a  2  s .com*/
    // we are running in the socket accept thread
    // wait until the event dispatcher gets us the events
    // or we die, what ever happens first
    try {
        l.lock.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    try {
        l.sock.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.v("Ending EventServer listener " + socket.getPort());
}

From source file:org.zaproxy.zap.extension.websocket.WebSocketProxy.java

/**
 * Factory method to create appropriate version.
 *
 * @param version Protocol version./* www.j  a va  2 s  .  c  o m*/
 * @param localSocket Channel from browser to ZAP.
 * @param remoteSocket Channel from ZAP to server.
 * @param subprotocol Provide null if there is no subprotocol specified.
 * @param extensions Map of negotiated extensions, null or empty list.
 * @throws WebSocketException
 * @return Version specific proxy object.
 * @see #create(String, Socket, Socket, String, int, String, Map)
 */
public static WebSocketProxy create(String version, Socket localSocket, Socket remoteSocket, String subprotocol,
        Map<String, String> extensions) throws WebSocketException {
    return create(version, localSocket, remoteSocket, remoteSocket.getInetAddress().getHostName(),
            remoteSocket.getPort(), subprotocol, extensions);
}

From source file:com.sittinglittleduck.DirBuster.EasySSLProtocolSocketFactoryUnitTest.java

@Test
public void shouldCreateSocketForGivenHostAndPort() throws Exception {
    // Given/*www  . jav a2 s. co m*/
    String host = "localhost";
    int port = 18080;
    // When
    Socket sslSocket = socketFactory.createSocket(host, port);
    // Then
    assertThat(sslSocket.getInetAddress().getHostName(), is(equalTo(host)));
    assertThat(sslSocket.getPort(), is(equalTo(port)));
}