Example usage for java.net InetSocketAddress InetSocketAddress

List of usage examples for java.net InetSocketAddress InetSocketAddress

Introduction

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

Prototype

private InetSocketAddress(int port, String hostname) 

Source Link

Usage

From source file:com.bitsofproof.supernode.core.FixedAddressDiscovery.java

@Override
public List<InetSocketAddress> discover() {
    List<InetSocketAddress> al = new ArrayList<InetSocketAddress>();
    try {/* w  ww .ja va 2  s.c  om*/
        String[] split = connectTo.split(":");
        if (split.length == 2) {
            al.add(new InetSocketAddress(InetAddress.getByName(split[0]), Integer.valueOf(split[1])));
        } else {
            al.add(new InetSocketAddress(InetAddress.getByName(split[0]), chain.getPort()));
        }
    } catch (Exception e) {
        log.error("can not connect to " + connectTo, e);
    }
    return al;
}

From source file:net.arccotangent.pacchat.net.Client.java

public static void sendMessage(String msg, String ip_address) {
    client_log.i("Sending message to " + ip_address);
    client_log.i("Connecting to server...");

    PublicKey pub;//from  www. j  a  v  a2s  .c  om
    PrivateKey priv;
    Socket socket;
    BufferedReader input;
    BufferedWriter output;

    client_log.i("Checking for recipient's public key...");
    if (KeyManager.checkIfIPKeyExists(ip_address)) {
        client_log.i("Public key found.");
    } else {
        client_log.i("Public key not found, requesting key from their server.");
        try {
            Socket socketGetkey = new Socket();
            socketGetkey.connect(new InetSocketAddress(InetAddress.getByName(ip_address), Server.PORT), 1000);
            BufferedReader inputGetkey = new BufferedReader(
                    new InputStreamReader(socketGetkey.getInputStream()));
            BufferedWriter outputGetkey = new BufferedWriter(
                    new OutputStreamWriter(socketGetkey.getOutputStream()));

            outputGetkey.write("301 getkey");
            outputGetkey.newLine();
            outputGetkey.flush();

            String pubkeyB64 = inputGetkey.readLine();
            byte[] pubEncoded = Base64.decodeBase64(pubkeyB64);
            X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubEncoded);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");

            outputGetkey.close();
            inputGetkey.close();

            KeyManager.saveKeyByIP(ip_address, keyFactory.generatePublic(pubSpec));
        } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
            client_log.e("Error saving recipient's key!");
            e.printStackTrace();
        }
    }

    try {
        socket = new Socket();
        socket.connect(new InetSocketAddress(InetAddress.getByName(ip_address), Server.PORT), 1000);
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    } catch (SocketTimeoutException e) {
        client_log.e("Connection to server timed out!");
        e.printStackTrace();
        return;
    } catch (ConnectException e) {
        client_log.e("Connection to server was refused!");
        e.printStackTrace();
        return;
    } catch (UnknownHostException e) {
        client_log.e("You entered an invalid IP address!");
        e.printStackTrace();
        return;
    } catch (IOException e) {
        client_log.e("Error connecting to server!");
        e.printStackTrace();
        return;
    }

    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    pub = KeyManager.loadKeyByIP(ip_address);
    priv = Main.getKeypair().getPrivate();

    String cryptedMsg = MsgCrypto.encryptAndSignMessage(msg, pub, priv);
    try {
        client_log.i("Sending message to recipient.");
        output.write("200 encrypted message");
        output.newLine();
        output.write(cryptedMsg);
        output.newLine();
        output.flush();

        String ack = input.readLine();
        switch (ack) {
        case "201 message acknowledgement":
            client_log.i("Transmission successful, received server acknowledgement.");
            break;
        case "202 unable to decrypt":
            client_log.e(
                    "Transmission failure! Server reports that the message could not be decrypted. Did your keys change? Asking recipient for key update.");
            kuc_id++;
            KeyUpdateClient kuc = new KeyUpdateClient(kuc_id, ip_address);
            kuc.start();
            break;
        case "203 unable to verify":
            client_log.w("**********************************************");
            client_log.w(
                    "Transmission successful, but the receiving server reports that the authenticity of the message could not be verified!");
            client_log.w(
                    "Someone may be tampering with your connection! This is an unlikely, but not impossible scenario!");
            client_log.w(
                    "If you are sure the connection was not tampered with, consider requesting a key update.");
            client_log.w("**********************************************");
            break;
        case "400 invalid transmission header":
            client_log.e(
                    "Transmission failure! Server reports that the message is invalid. Try updating your software and have the recipient do the same. If this does not fix the problem, report the error to developers.");
            break;
        default:
            client_log.w("Server responded with unexpected code: " + ack);
            client_log.w("Transmission might not have been successful.");
            break;
        }

        output.close();
        input.close();
    } catch (IOException e) {
        client_log.e("Error sending message to recipient!");
        e.printStackTrace();
    }
}

From source file:at.bitfire.davdroid.webdav.TlsSniSocketFactoryTest.java

@Override
protected void setUp() {
    // sni.velox.ch is used to test SNI (without SNI support, the certificate is invalid)
    sampleTlsEndpoint = new InetSocketAddress("sni.velox.ch", 443);
}

From source file:com.andrewkroh.cicso.rtp.Destination.java

public Destination(String host, int port) {
    Preconditions.checkNotNull(host, "Host cannot be null.");
    Preconditions.checkArgument(port > 0 && port <= 65535, "Port number <%s> is not within (0, 65535].", port);

    this.host = host;
    this.port = port;
    this.socketAddress = new InetSocketAddress(host, port);
}

From source file:com.zextras.zimbradrive.statustest.ConnectionTestUtils.java

public boolean pingHost(URL url, int connectionTimeout) throws MalformedURLException {
    String host = url.getHost();/*www  .  j av  a2  s. co m*/
    int port = url.getPort();
    if (port == -1) {
        if (url.getProtocol().equals("https")) {
            port = 443;
        } else {
            port = 80;
        }
    }
    try (Socket socket = new Socket()) {
        InetSocketAddress inetSocketAddress = new InetSocketAddress(host, port);
        socket.connect(inetSocketAddress, connectionTimeout);
        return true;
    } catch (IOException e) {
        return false; // Either timeout or unreachable or failed DNS lookup.
    }
}

From source file:com.hellblazer.jackal.testUtil.gossip.GossipDiscoveryNode1Cfg.java

@Bean(name = "gossipEndpoint")
public InetSocketAddress gossipEndpoint() {
    return new InetSocketAddress("127.0.0.1", GossipTestCfg.getTestPort1());
}

From source file:com.hellblazer.jackal.testUtil.gossip.GossipDiscoveryNode2Cfg.java

@Bean(name = "gossipEndpoint")
public InetSocketAddress gossipEndpoint() {
    return new InetSocketAddress("127.0.0.1", GossipTestCfg.getTestPort2());
}

From source file:com.clustercontrol.util.EndpointManager.java

public static void setProxy(String proxyHost, int proxyPort) {
    Proxy proxy = null;//from   w ww  .jav  a  2s  .  co m
    if (proxyHost == null || proxyHost.length() == 0) {
        proxy = Proxy.NO_PROXY;
    } else {
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    }
    getInstance().proxy = proxy;
}

From source file:net.arccotangent.pacchat.net.KeyUpdateClient.java

public void run() {
    Socket socket;/*from  w  w w .jav  a 2  s .c o  m*/
    BufferedReader input;
    BufferedWriter output;

    kuc_log.i("Connecting to server at " + server_ip);

    try {
        socket = new Socket();
        socket.connect(new InetSocketAddress(InetAddress.getByName(server_ip), Server.PORT), 1000);
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    } catch (SocketTimeoutException e) {
        kuc_log.e("Connection to server timed out!");
        e.printStackTrace();
        return;
    } catch (ConnectException e) {
        kuc_log.e("Connection to server was refused!");
        e.printStackTrace();
        return;
    } catch (UnknownHostException e) {
        kuc_log.e("You entered an invalid IP address!");
        e.printStackTrace();
        return;
    } catch (IOException e) {
        kuc_log.e("Error connecting to server!");
        e.printStackTrace();
        return;
    }

    try {
        kuc_log.i("Requesting a key update.");
        output.write("302 request key update");
        output.newLine();
        output.flush();

        kuc_log.i("Awaiting response from server.");
        String update = input.readLine();
        switch (update) {
        case "303 update":
            kuc_log.i("Server accepted update request, sending public key.");
            String pubkeyB64 = Base64.encodeBase64String(Main.getKeypair().getPublic().getEncoded());
            output.write(pubkeyB64);
            output.newLine();
            output.flush();
            output.close();
            break;
        case "304 no update":
            kuc_log.i("Server rejected update request, closing connection.");
            input.close();
            output.close();
            break;
        case "305 update unavailable":
            kuc_log.i("Server cannot update at this time, try again later.");
            input.close();
            output.close();
            break;
        default:
            kuc_log.i("Server sent back invalid response");
            input.close();
            output.close();
            break;
        }
    } catch (IOException e) {
        kuc_log.e("Error in key update request!");
        e.printStackTrace();
    }
}

From source file:com.wealdtech.jackson.modules.InetSocketAddressDeserializer.java

@Override
protected InetSocketAddress _deserialize(String value, DeserializationContext ctxt) throws IOException {
    // TODO check for malformed values
    Iterator<String> vals = Splitter.on(':').split(value).iterator();
    final String host = vals.next();
    final int port = Integer.parseInt(vals.next());
    return new InetSocketAddress(host, port);
}