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:Test.java

private static void clientStart() {
    try {//from w w w .  j a v  a2  s.c  o  m
        InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583);
        AsynchronousSocketChannel clientSocketChannel = AsynchronousSocketChannel.open();
        Future<Void> connectFuture = clientSocketChannel.connect(hostAddress);
        connectFuture.get(); // Wait until connection is done.
        OutputStream os = Channels.newOutputStream(clientSocketChannel);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        for (int i = 0; i < 5; i++) {
            oos.writeObject("Look at me " + i);
            Thread.sleep(1000);
        }
        oos.writeObject("EOF");
        oos.close();
        clientSocketChannel.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Utils.java

private static void installProxySelector(final String hostName, final int portNum) {
    ProxySelector.setDefault(new ProxySelector() {
        public List select(URI uri) {
            List list = new ArrayList();
            list.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostName, portNum)));
            return list;
        }//from   w w  w .  j a  va2  s .c  o  m

        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        }
    });
}

From source file:guiTool.Helper.java

public static final boolean checkInternet() {

    try {//from   ww  w .jav a2  s . c  o m
        Socket socket = new Socket();
        InetSocketAddress add = new InetSocketAddress("www.google.com", 80);
        socket.connect(add, 3000);
        return true;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "The server or your internet connection could be down.",
                "connection test", JOptionPane.ERROR_MESSAGE);
        return false;
    }

}

From source file:Main.java

/**
 * Input is "daddy[8880],sindhu[8880],camille[5555]. Return List of
 * InetSocketAddress//from w ww.j a  v  a 2  s . c om
 */
public static List<InetSocketAddress> parseCommaDelimitedHosts2(String hosts, int port_range) {

    StringTokenizer tok = new StringTokenizer(hosts, ",");
    String t;
    InetSocketAddress addr;
    Set<InetSocketAddress> retval = new HashSet<InetSocketAddress>();

    while (tok.hasMoreTokens()) {
        t = tok.nextToken().trim();
        String host = t.substring(0, t.indexOf('['));
        host = host.trim();
        int port = Integer.parseInt(t.substring(t.indexOf('[') + 1, t.indexOf(']')));
        for (int i = port; i < port + port_range; i++) {
            addr = new InetSocketAddress(host, i);
            retval.add(addr);
        }
    }
    return new LinkedList<InetSocketAddress>(retval);
}

From source file:net.jadler.stubbing.server.jetty.RequestUtils.java

public static Request convert(HttpServletRequest source) throws IOException {
    String method = source.getMethod();
    URI requestUri = URI.create(source.getRequestURL() + getQueryString(source));
    InputStream body = source.getInputStream();
    InetSocketAddress localAddress = new InetSocketAddress(source.getLocalAddr(), source.getLocalPort());
    InetSocketAddress remoteAddress = new InetSocketAddress(source.getRemoteAddr(), source.getRemotePort());
    String encoding = source.getCharacterEncoding();
    Map<String, List<String>> headers = converHeaders(source);
    return new Request(method, requestUri, headers, body, localAddress, remoteAddress, encoding);
}

From source file:Main.java

public static InetSocketAddress parseSocketAddress(String socketAddress) {

    if (socketAddress != null) {
        int colonIdx = socketAddress.indexOf(':');
        if (colonIdx != -1) {
            String hostName = socketAddress.substring(0, colonIdx);
            String port = socketAddress.substring(colonIdx + 1);
            return new InetSocketAddress(hostName, Integer.parseInt(port));
        }/* w w  w. j a v  a2  s  . c o m*/
    }

    return null;
}

From source file:com.mirth.connect.server.util.ConnectorUtil.java

public static ConnectionTestResponse testConnection(String host, int port, int timeout, String localAddr,
        int localPort) throws Exception {
    Socket socket = null;//ww  w  .j  a  v a 2s.  co m
    InetSocketAddress address = null;
    InetSocketAddress localAddress = null;

    try {
        address = new InetSocketAddress(host, port);

        if (StringUtils.isBlank(address.getAddress().getHostAddress()) || (address.getPort() < 0)
                || (address.getPort() > 65534)) {
            throw new Exception();
        }
    } catch (Exception e) {
        return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, "Invalid host or port.");
    }

    if (localAddr != null) {
        try {
            localAddress = new InetSocketAddress(localAddr, localPort);

            if (StringUtils.isBlank(localAddress.getAddress().getHostAddress()) || (localAddress.getPort() < 0)
                    || (localAddress.getPort() > 65534)) {
                throw new Exception();
            }
        } catch (Exception e) {
            return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE,
                    "Invalid local host or port.");
        }
    }

    try {
        socket = new Socket();

        if (localAddress != null) {
            try {
                socket.bind(localAddress);
            } catch (Exception e) {
                return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE,
                        "Could not bind to local address: " + localAddress.getAddress().getHostAddress() + ":"
                                + localAddress.getPort());
            }
        }

        socket.connect(address, timeout);
        String connectionInfo = socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort() + " -> "
                + address.getAddress().getHostAddress() + ":" + address.getPort();
        return new ConnectionTestResponse(ConnectionTestResponse.Type.SUCCESS,
                "Successfully connected to host: " + connectionInfo, connectionInfo);
    } catch (SocketTimeoutException ste) {
        return new ConnectionTestResponse(ConnectionTestResponse.Type.TIME_OUT, "Timed out connecting to host: "
                + address.getAddress().getHostAddress() + ":" + address.getPort());
    } catch (Exception e) {
        return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, "Could not connect to host: "
                + address.getAddress().getHostAddress() + ":" + address.getPort());
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
}

From source file:cloudfoundry.norouter.NorouterProperties.java

private static String detectHostAddress() {
    try {/*from  www  . j  a  va 2  s  .  c  om*/
        final DatagramChannel channel = DatagramChannel.open().connect(new InetSocketAddress("8.8.8.8", 1));
        return ((InetSocketAddress) channel.getLocalAddress()).getHostString();
    } catch (IOException e) {
        return null;
    }
}

From source file:com.orange.clara.pivotaltrackermirror.util.ProxyUtil.java

public static Proxy getProxy() throws Exception {
    if (PROXY_HOST == null) {
        return null;
    }//from   ww w.ja v a2 s.  com
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
    if (PROXY_USER == null) {
        return proxy;
    }
    loadAuthenticator();
    return proxy;
}

From source file:example.springdata.cassandra.util.CassandraSocket.java

/**
 * @param host must not be {@literal null} or empty.
 * @param port//  w  w  w  . j  a  va  2 s  . c  om
 * @return {@literal true} if the TCP port accepts a connection.
 */
public static boolean isConnectable(String host, int port) {

    Assert.hasText(host, "Host must not be null or empty!");

    try (Socket socket = new Socket()) {

        socket.setSoLinger(true, 0);
        socket.connect(new InetSocketAddress(host, port),
                (int) TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS));

        return true;

    } catch (Exception e) {
        return false;
    }
}