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.devoteam.srit.xmlloader.http.nio.NIOChannelHttp.java

/** Open a connexion to each Stack */
public synchronized boolean open() throws Exception {
    if (this.secure) {
        StatPool.beginStatisticProtocol(StatPool.CHANNEL_KEY, StatPool.NIO_KEY, StackFactory.PROTOCOL_TLS,
                StackFactory.PROTOCOL_HTTP);
    } else {/*from   ww  w.  ja v  a  2s  .c  om*/
        StatPool.beginStatisticProtocol(StatPool.CHANNEL_KEY, StatPool.NIO_KEY, StackFactory.PROTOCOL_TCP,
                StackFactory.PROTOCOL_HTTP);
    }

    if (null == this.socketServerHttp) {
        nbOpens++;

        if (nbOpens > 3) {
            throw new Exception("HTTP Channel already failed to open more than 3 times");
        }

        String host = this.getRemoteHost();
        int port = this.getRemotePort();

        DefaultHttpClientConnection defaultHttpClientConnection = new DefaultHttpClientConnection();

        //
        // Bind the socket to the local address
        //
        String localHost = this.getLocalHost();
        int localPort = initialLocalport;

        if (null == localHost)
            localHost = "0.0.0.0";

        InetSocketAddress localsocketAddress = new InetSocketAddress(localHost, localPort);
        InetSocketAddress remoteAddress = new InetSocketAddress(host, port);

        this.socketClientHttp = new NIOSocketClientHttp();
        Socket socket = new HybridSocket((NIOSocketClientHttp) this.socketClientHttp);

        if (secure)
            StackHttp.ioReactor.openTLS(localsocketAddress, remoteAddress, (HybridSocket) socket,
                    StackHttp.context);
        else
            StackHttp.ioReactor.openTCP(localsocketAddress, remoteAddress, (HybridSocket) socket);

        defaultHttpClientConnection.bind(socket, new BasicHttpParams());
        ((NIOSocketClientHttp) this.socketClientHttp).init(defaultHttpClientConnection, this);

        // read all properties for the TCP socket 
        Config.getConfigForTCPSocket(socket, false);
    }
    return true;
}

From source file:com.verisign.storm.metrics.GraphiteMetricsConsumerTest.java

@BeforeClass
private void setupTestFixtures() {
    try {/*from   w  w  w . ja  v a  2 s .c o m*/
        testGraphiteServerSocketAddr = new InetSocketAddress(testGraphiteHost, testGraphitePort);
        testGraphiteServer = ServerSocketChannel.open();
        testGraphiteServer.socket().bind(testGraphiteServerSocketAddr);
        testGraphiteServer.configureBlocking(false);
    } catch (IOException e) {
        LOG.error("Failed to open Graphite server at {}:{}", testGraphiteHost, testGraphitePort);
    }
}

From source file:com.barchart.http.server.TestHttpServer.java

@Before
public void setUp() throws Exception {

    server = new HttpServer();

    basic = new TestRequestHandler("basic", false, 0, 0, false, false);
    async = new TestRequestHandler("async", true, 0, 0, false, false);
    asyncDelayed = new TestRequestHandler("async-delayed", true, 50, 0, false, false);
    clientDisconnect = new TestRequestHandler("", true, 500, 500, false, false);
    error = new TestRequestHandler("error", false, 0, 0, true, false);
    channelError = new TestRequestHandler("channel-error", false, 0, 0, false, true);

    infoHandler = new TestRequestHandler("info", false, 0, 0, false, false);
    serviceHandler = new TestRequestHandler("service", false, 0, 0, false, false);

    final ServerSocket s = new ServerSocket(0);
    port = s.getLocalPort();/* w  w w. ja va 2s .c o  m*/
    s.close();

    final HttpServerConfig config = new HttpServerConfig().requestHandler("/basic", basic)
            .address(new InetSocketAddress("localhost", port)).parentGroup(new NioEventLoopGroup(1))
            .childGroup(new NioEventLoopGroup(1)).requestHandler("/async", async)
            .requestHandler("/async-delayed", asyncDelayed)
            .requestHandler("/client-disconnect", clientDisconnect)
            .requestHandler("/channel-error", channelError).requestHandler("/error", error)
            .requestHandler("/service/info", infoHandler).requestHandler("/service", serviceHandler)
            .maxConnections(1);

    server.configure(config).listen().sync();

    client = new DefaultHttpClient(new PoolingClientConnectionManager());

}

From source file:org.messic.server.api.tagwizard.discogs.DiscogsTAGWizardPlugin.java

private Proxy getProxy() {
    if (this.configuration != null) {
        String url = (String) this.configuration.get("proxy-url");
        String port = (String) this.configuration.get("proxy-port");
        if (url != null && port != null && url.length() > 0 && port.length() > 0) {
            SocketAddress addr = new InetSocketAddress(url, Integer.valueOf(port));
            Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
            return proxy;
        }/* w ww  . j a  v a  2s  .co m*/
    }
    return null;
}

From source file:com.hurence.logisland.redis.service.ITRedisKeyValueCacheClientService.java

private int getAvailablePort() throws IOException {
    try (SocketChannel socket = SocketChannel.open()) {
        socket.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        socket.bind(new InetSocketAddress("localhost", 0));
        return socket.socket().getLocalPort();
    }//from  w w w. j  av a2 s  .co m
}

From source file:org.red5.server.net.policy.SocketPolicyHandler.java

@Override
public void afterPropertiesSet() throws Exception {
    log.debug("Starting socket policy file server");
    try {/*from  w  ww. j a v  a2s .  co  m*/
        // get the file
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(policyFilePath);
        if (is != null) {
            //read the policy file
            policyData = IoBuffer.allocate(1024);
            byte[] b = new byte[4];
            while (is.read(b) != -1) {
                policyData.put(b);
            }
            policyData.flip();
            is.close();
            log.info("Policy file read successfully");
            // create accept socket
            acceptor = new NioSocketAcceptor();
            acceptor.setHandler(this);
            Set<SocketAddress> addresses = new HashSet<SocketAddress>();
            addresses.add(new InetSocketAddress(host, port));
            acceptor.bind(addresses);
            log.info("Socket policy file server listening on port {}", port);
        } else {
            log.error("Policy file was not found");
        }
    } catch (Exception e) {
        log.error("Exception initializing socket policy server", e);
    }
}

From source file:eu.stratosphere.nephele.net.NetUtils.java

/**
 * Util method to build socket addr from either:
 * <host>//from w w  w .java2 s .co m
 * <host>:<post>
 * <fs>://<host>:<port>/<path>
 */
public static InetSocketAddress createSocketAddr(String target, int defaultPort) {
    int colonIndex = target.indexOf(':');
    if (colonIndex < 0 && defaultPort == -1) {
        throw new RuntimeException("Not a host:port pair: " + target);
    }
    String hostname = "";
    int port = -1;
    if (!target.contains("/")) {
        if (colonIndex == -1) {
            hostname = target;
        } else {
            // must be the old style <host>:<port>
            hostname = target.substring(0, colonIndex);
            port = Integer.parseInt(target.substring(colonIndex + 1));
        }
    } else {
        // a new uri
        try {
            URI addr = new URI(target);
            hostname = addr.getHost();
            port = addr.getPort();
        } catch (URISyntaxException use) {
            LOG.fatal(use);
        }
    }

    if (port == -1) {
        port = defaultPort;
    }

    if (getStaticResolution(hostname) != null) {
        hostname = getStaticResolution(hostname);
    }
    return new InetSocketAddress(hostname, port);
}

From source file:com.manning.androidhacks.hack023.net.SimpleSSLSocketFactory.java

public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {
        if (localPort < 0) {
            localPort = 0;/*from  w  w w .j  a v a 2s.  c om*/
        }
        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sslsock.bind(isa);
    }

    sslsock.connect(remoteAddress, connTimeout);
    sslsock.setSoTimeout(soTimeout);
    return sslsock;
}

From source file:com.googlecode.jmxtrans.model.output.SensuWriterFactory.java

public SensuWriterFactory(@JsonProperty("typeNames") ImmutableList<String> typeNames,
        @JsonProperty("booleanAsNumber") boolean booleanAsNumber, @JsonProperty("host") String host,
        @JsonProperty("port") Integer port, @JsonProperty("rootPrefix") String rootPrefix,
        @JsonProperty("flushStrategy") String flushStrategy,
        @JsonProperty("flushDelayInSeconds") Integer flushDelayInSeconds,
        @JsonProperty("poolSize") Integer poolSize) {
    this.rootPrefix = rootPrefix;
    this.typeNames = firstNonNull(typeNames, ImmutableList.<String>of());
    this.booleanAsNumber = booleanAsNumber;
    this.server = new InetSocketAddress(firstNonNull(host, "localhost"), firstNonNull(port, 3030));
    this.flushStrategy = createFlushStrategy(flushStrategy, flushDelayInSeconds);
    this.poolSize = firstNonNull(poolSize, 1);
}

From source file:org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPoolTest.java

public void testConstructorUnspecifiedSSLPort() throws Exception {
    NioHttpCommandConnectionPool pool = new NioHttpCommandConnectionPool(null, null, null, null,
            createNiceMock(AsyncNHttpClientHandler.class), null, createNiceMock(HttpParams.class),
            URI.create("https://localhost"));
    assertEquals(pool.getTarget(), new InetSocketAddress("localhost", 443));
}