List of usage examples for java.net InetSocketAddress InetSocketAddress
private InetSocketAddress(int port, String hostname)
From source file:com.aujur.ebookreader.ssl.EasySSLSocketFactory.java
/** * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, java.lang.String, int, * java.net.InetAddress, int, org.apache.http.params.HttpParams) *///from ww w. java 2 s . c o m 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)) { // we need to bind explicitly if (localPort < 0) { localPort = 0; // indicates "any" } InetSocketAddress isa = new InetSocketAddress(localAddress, localPort); sslsock.bind(isa); } sslsock.connect(remoteAddress, connTimeout); sslsock.setSoTimeout(soTimeout); return sslsock; }
From source file:org.zalando.stups.spring.http.client.ClientHttpRequestFactorySelector.java
public static ClientHttpRequestFactory getRequestFactory(TimeoutConfig timeoutConfig) { Properties properties = System.getProperties(); String proxyHost = properties.getProperty("http.proxyHost"); int proxyPort = properties.containsKey("http.proxyPort") ? Integer.valueOf(properties.getProperty("http.proxyPort")) : 80;/* ww w .ja v a 2s .c o m*/ if (HTTP_COMPONENTS_AVAILABLE) { HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) HttpComponentsClientRequestFactoryCreator .createRequestFactory(proxyHost, proxyPort); factory.setReadTimeout(timeoutConfig.getReadTimeout()); factory.setConnectTimeout(timeoutConfig.getConnectTimeout()); factory.setConnectionRequestTimeout(timeoutConfig.getConnectionRequestTimeout()); return factory; } else { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(timeoutConfig.getConnectTimeout()); requestFactory.setReadTimeout(timeoutConfig.getReadTimeout()); if (proxyHost != null) { requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))); } return requestFactory; } }
From source file:com.iveely.framework.net.AsynClient.java
/** * Send message to server./* www. j ava2 s . co m*/ */ public boolean send(Packet packet) { ConnectFuture future = this.connector.connect(new InetSocketAddress(this.ipAddress, this.port)); try { byte[] msg = SerializationUtils.serialize(packet); future.awaitUninterruptibly(); future.getSession().write(msg); return true; } catch (RuntimeIoException e) { e.printStackTrace(); if (e.getCause() instanceof ConnectException) { try { if (future.isConnected()) { future.getSession().close(); } } catch (RuntimeIoException e1) { e1.printStackTrace(); } } } return false; }
From source file:edu.umass.cs.gigapaxos.testing.TESTPaxosNode.java
private PaxosManager<Integer> startPaxosManagerAndApp(int id, NodeConfig<Integer> nc, boolean local) { try {/* w w w. ja v a2 s. c o m*/ // shared between app and paxos manager only for testing JSONMessenger<Integer> niot = null; this.pm = new PaxosManager<Integer>(id, nc, (niot = new JSONMessenger<Integer>(new MessageNIOTransport<Integer, JSONObject>(id, nc, new PacketDemultiplexerDefault(), true, SSLDataProcessingWorker.SSL_MODES .valueOf(Config.getGlobal(PC.SERVER_SSL_MODE).toString())))), (this.app = new TESTPaxosApp(niot)), null, true); pm.initClientMessenger(new InetSocketAddress(nc.getNodeAddress(myID), nc.getNodePort(myID)), niot); } catch (IOException ioe) { ioe.printStackTrace(); } return pm; }
From source file:mdretrieval.FileFetcher.java
public static String[] loadStringFromURL(String destinationURL, boolean acceptRDF) throws IOException { String[] ret = new String[2]; HttpURLConnection urlConnection = null; InputStream inputStream = null; String dest = destinationURL; URL url = new URL(dest); Proxy proxy = null;/*from ww w . ja v a 2s . co m*/ if (ServerConstants.isProxyEnabled) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ServerConstants.hostname, ServerConstants.port)); urlConnection = (HttpURLConnection) url.openConnection(proxy); } else { urlConnection = (HttpURLConnection) url.openConnection(); } boolean redirect = false; int status = urlConnection.getResponseCode(); if (Master.DEBUG_LEVEL >= Master.LOW) System.out.println("RESPONSE-CODE--> " + status); if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) redirect = true; } if (redirect) { String newUrl = urlConnection.getHeaderField("Location"); dest = newUrl; urlConnection.disconnect(); if (Master.DEBUG_LEVEL > Master.LOW) System.out.println("REDIRECT--> " + newUrl); urlConnection = openMaybeProxyConnection(proxy, newUrl); } try { urlConnection.setRequestMethod("GET"); urlConnection.setRequestProperty("Accept", HTTP_RDFXML_PROP); urlConnection.setDoInput(true); //urlConnection.setDoOutput(true); inputStream = urlConnection.getInputStream(); ret[1] = urlConnection.getHeaderField("Content-Type"); } catch (IllegalStateException e) { if (Master.DEBUG_LEVEL >= Master.LOW) System.out.println(" DEBUG: IllegalStateException"); urlConnection.disconnect(); HttpURLConnection conn2 = openMaybeProxyConnection(proxy, dest); conn2.setRequestMethod("GET"); conn2.setRequestProperty("Accept", HTTP_RDFXML_PROP); conn2.setDoInput(true); inputStream = conn2.getInputStream(); ret[1] = conn2.getHeaderField("Content-Type"); } try { ret[0] = IOUtils.toString(inputStream); if (Master.DEBUG_LEVEL > Master.LOW) { System.out.println(" Content-type: " + urlConnection.getHeaderField("Content-Type")); } } finally { IOUtils.closeQuietly(inputStream); urlConnection.disconnect(); } if (Master.DEBUG_LEVEL > Master.LOW) System.out.println("Done reading " + destinationURL); return ret; }
From source file:com.hellblazer.jackal.configuration.basic.LocalGossipConfiguration.java
@Bean(name = "seedHosts") public List<InetSocketAddress> seedHosts() throws UnknownHostException { return asList(new InetSocketAddress("127.0.0.1", 1024)); }
From source file:com.google.cloud.hadoop.util.HttpTransportFactory.java
/** * Create an {@link HttpTransport} based on an type class and an optional HTTP proxy. * * @param type The type of HttpTransport to use. * @param proxyAddress The HTTP proxy to use with the transport. Of the form hostname:port. If * empty no proxy will be used.//from w w w .j av a2 s .com * @return The resulting HttpTransport. * @throws IllegalArgumentException If the proxy address is invalid. * @throws IOException If there is an issue connecting to Google's Certification server. */ public static HttpTransport createHttpTransport(HttpTransportType type, @Nullable String proxyAddress) throws IOException { try { URI proxyUri = parseProxyAddress(proxyAddress); switch (type) { case APACHE: HttpHost proxyHost = null; if (proxyUri != null) { proxyHost = new HttpHost(proxyUri.getHost(), proxyUri.getPort()); } return createApacheHttpTransport(proxyHost); case JAVA_NET: Proxy proxy = null; if (proxyUri != null) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); } return createNetHttpTransport(proxy); default: throw new IllegalArgumentException(String.format("Invalid HttpTransport type '%s'", type.name())); } } catch (GeneralSecurityException e) { throw new IOException(e); } }
From source file:com.ntsync.android.sync.client.MySSLSocketFactory.java
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, HttpParams params) throws IOException { 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)) { // we need to bind explicitly int validLocalPort = localPort; if (validLocalPort < 0) { validLocalPort = 0; // indicates "any" }/*from w ww . j a va 2 s .c o m*/ InetSocketAddress isa = new InetSocketAddress(localAddress, validLocalPort); sslsock.bind(isa); } sslsock.connect(remoteAddress, connTimeout); sslsock.setSoTimeout(soTimeout); return sslsock; }
From source file:com.pinterest.secor.common.ZookeeperConnector.java
private Iterable<InetSocketAddress> getZookeeperAddresses() { String zookeeperQuorum = mConfig.getZookeeperQuorum(); String[] hostports = zookeeperQuorum.split(","); LinkedList<InetSocketAddress> result = new LinkedList<InetSocketAddress>(); for (String hostport : hostports) { String[] elements = hostport.split(":"); assert elements.length == 2 : Integer.toString(elements.length) + " == 2"; String host = elements[0]; int port = Integer.parseInt(elements[1]); result.add(new InetSocketAddress(host, port)); }//from w w w. j a va 2 s .co m return result; }
From source file:cn.mimessage.and.sdk.net.EasySSLSocketFactory.java
/** * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, java.lang.String, int, java.net.InetAddress, int, * org.apache.http.params.HttpParams) */// ww w. j ava2 s . c o m @Override 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)) { // we need to bind explicitly if (localPort < 0) { localPort = 0; // indicates "any" } InetSocketAddress isa = new InetSocketAddress(localAddress, localPort); sslsock.bind(isa); } sslsock.connect(remoteAddress, connTimeout); sslsock.setSoTimeout(soTimeout); return sslsock; }