List of usage examples for java.net Socket connect
public void connect(SocketAddress endpoint, int timeout) throws IOException
From source file:org.sakaiproject.antivirus.impl.ClamAVScanner.java
protected Socket getClamdSocket() throws UnknownHostException { InetAddress address = InetAddress.getByName(getHost()); Socket socket = null; try {/* w w w .java 2s. c o m*/ socket = new Socket(); SocketAddress socketAddress = new InetSocketAddress(address, getPort()); socket.connect(socketAddress, 5000); if (!socket.isConnected()) { return null; } return socket; } catch (IOException ioe) { logger.error("Exception caught acquiring main socket to CLAMD on " + address + " on port " + getPort() + ": " + ioe.getMessage()); } return socket; }
From source file:at.alladin.rmbt.android.util.CheckIpTask.java
@Override protected JSONArray doInBackground(final Void... params) { needsRetry = false;//from w w w . ja va2 s . com serverConn = new ControlServerConnection(activity); try { Socket s = new Socket(); InetSocketAddress addr = new InetSocketAddress( ConfigHelper.getCachedControlServerNameIpv4(activity.getApplicationContext()), ConfigHelper.getControlServerPort(activity.getApplicationContext())); s.connect(addr, 5000); privateIpv4 = s.getLocalAddress(); s.close(); } catch (Exception e) { e.printStackTrace(); } try { Socket s = new Socket(); InetSocketAddress addr = new InetSocketAddress( ConfigHelper.getCachedControlServerNameIpv6(activity.getApplicationContext()), ConfigHelper.getControlServerPort(activity.getApplicationContext())); s.connect(addr, 5000); privateIpv6 = s.getLocalAddress(); s.close(); } catch (SocketTimeoutException e) { e.printStackTrace(); needsRetry = ConfigHelper.isRetryRequiredOnIpv6SocketTimeout(activity); } catch (Exception e) { needsRetry = false; e.printStackTrace(); } newsList = new JSONArray(); if (privateIpv4 != null) { JSONArray response = serverConn.requestIp(false); if (response != null && response.length() >= 1) { newsList.put(response.opt(0)); } } else { Log.d(DEBUG_TAG, "no private ipv4 found"); } if (privateIpv6 != null) { JSONArray response = serverConn.requestIp(true); if (response != null && response.length() >= 1) { newsList.put(response.opt(0)); } } else { Log.d(DEBUG_TAG, "no private ipv6 found"); } return newsList; }
From source file:com.owncloud.android.authenticator.EasySSLSocketFactory.java
/** * Attempts to get a new socket connection to the given host within the * given time limit./* ww w . j a v a2s .c om*/ * <p> * To circumvent the limitations of older JREs that do not support connect * timeout a controller thread is executed. The controller thread attempts * to create a new socket within the given limit of time. If socket * constructor does not return until the timeout expires, the controller * terminates and throws an {@link ConnectTimeoutException} * </p> * * @param host the host name/IP * @param port the port on the host * @param clientHost the local host name/IP to bind the socket to * @param clientPort the port on the local machine * @param params {@link HttpConnectionParams Http connection parameters} * * @return Socket a new socket * * @throws IOException if an I/O error occurs while creating the socket * @throws UnknownHostException if the IP address of the host cannot be * determined */ public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { Log.d(TAG, "Creating SSL Socket with remote " + host + ":" + port + ", local " + localAddress + ":" + localPort + ", params: " + params); if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); /*if (timeout == 0) { Log.d(TAG, " ... with connection timeout 0 and socket timeout " + params.getSoTimeout()); Socket socket = socketfactory.createSocket(host, port, localAddress, localPort); socket.setSoTimeout(params.getSoTimeout()); return socket; } else {*/ Log.d(TAG, " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout()); Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.setSoTimeout(params.getSoTimeout()); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; //} }
From source file:com.predic8.membrane.core.transport.ssl.SSLContext.java
public Socket createSocket(InetAddress host, int port, InetAddress addr, int localPort, int connectTimeout) throws IOException { Socket s = new Socket(); s.bind(new InetSocketAddress(addr, localPort)); s.connect(new InetSocketAddress(host, port), connectTimeout); SSLSocketFactory sslsf = sslc.getSocketFactory(); SSLSocket ssls = (SSLSocket) sslsf.createSocket(s, host.getHostName(), port, true); applyCiphers(ssls);// w w w.java2 s . c o m if (protocols != null) { ssls.setEnabledProtocols(protocols); } else { String[] protocols = ssls.getEnabledProtocols(); Set<String> set = new HashSet<String>(); for (String protocol : protocols) { if (protocol.equals("SSLv3") || protocol.equals("SSLv2Hello")) { continue; } set.add(protocol); } ssls.setEnabledProtocols(set.toArray(new String[0])); } return ssls; }
From source file:org.apache.flink.runtime.taskmanager.TaskManager.java
private static boolean tryToConnect(InetAddress fromAddress, SocketAddress toSocket, int timeout) throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("Trying to connect to JobManager (" + toSocket + ") from local address " + fromAddress + " with timeout " + timeout); }/* www . j a v a 2 s. co m*/ boolean connectable = true; Socket socket = null; try { socket = new Socket(); SocketAddress bindP = new InetSocketAddress(fromAddress, 0); // 0 = let the OS choose the port on this // machine socket.bind(bindP); socket.connect(toSocket, timeout); } catch (Exception ex) { LOG.info("Failed to connect to JobManager from address '" + fromAddress + "': " + ex.getMessage()); if (LOG.isDebugEnabled()) { LOG.debug("Failed with exception", ex); } connectable = false; } finally { if (socket != null) { socket.close(); } } return connectable; }
From source file:com.qspin.qtaste.tcom.rlogin.RLogin.java
/** * Reboot the remote host by sending the reboot command and check that * the remote host is not accessible anymore. * @return true if success, false otherwise *///from w w w . ja v a 2 s. c om public boolean reboot() { if (!sendCommand("reboot")) { return false; } // wait 1 second try { Thread.sleep(1000); } catch (InterruptedException ex) { } disconnect(); // check that remote host is not accessible anymore // open a socket without any parameters. It hasn't been binded or connected Socket socket = new Socket(); try { // bind to a local ephemeral port socket.bind(null); socket.connect(new InetSocketAddress(remoteHost, RLoginClient.DEFAULT_PORT), 1); } catch (SocketTimeoutException e) { logger.info("Rebooted host " + remoteHost + " successfully"); return true; } catch (IOException e) { logger.error("Something went wrong while rebooting host:" + remoteHost); return false; } finally { try { socket.close(); } catch (IOException ex) { } socket = null; } // Expected to get an exception as the remote host should not be reachable anymore logger.error("Host " + remoteHost + " did not reboot as expected! Please check that no other rlogin client is connected!"); return false; }
From source file:gov.nist.toolkit.soap.axis2.AuthSSLProtocolSocketFactory.java
/** * Attempts to get a new socket connection to the given host within the given time limit. * <p>//from w ww . j av a2s . c o m * To circumvent the limitations of older JREs that do not support connect timeout a * controller thread is executed. The controller thread attempts to create a new socket * within the given limit of time. If socket constructor does not return until the * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException} * </p> * * @param host the host name/IP * @param port the port on the host * @param params {@link HttpConnectionParams Http connection parameters} * * @return Socket a new socket * * @throws IOException if an I/O error occurs while creating the socket * @throws UnknownHostException if the IP address of the host cannot be * determined */ public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); timeout = 0; // wjm SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } }
From source file:AuthSSLProtocolSocketFactory.java
/** * Attempts to get a new socket connection to the given host within the given time limit. * <p>//from w w w. j a va 2 s .co m * To circumvent the limitations of older JREs that do not support connect timeout a * controller thread is executed. The controller thread attempts to create a new socket * within the given limit of time. If socket constructor does not return until the * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException} * </p> * * @param host the host name/IP * @param port the port on the host * @param params {@link HttpConnectionParams Http connection parameters} * @return Socket a new socket * @throws java.io.IOException if an I/O error occurs while creating the socket * @throws java.net.UnknownHostException if the IP address of the host cannot be * determined */ public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } }
From source file:org.archive.wayback.resourcestore.locationdb.FileProxyServlet.java
private DataSource locationToDataSource(String location, long offset) throws IOException { DataSource ds = null;//from w w w. j av a2 s . c om if (location.startsWith("http://")) { URL url = new URL(location); String hostname = url.getHost(); int port = url.getPort(); if (port == -1) { port = 80; } byte GET[] = "GET".getBytes(); byte HTTP11[] = "HTTP/1.1".getBytes(); InetAddress addr = InetAddress.getByName(hostname); HttpRequestMessage requestMessage = new HttpRequestMessage(GET, url.getFile().getBytes(), HTTP11); ANVLRecord headers = new ANVLRecord(); headers.addLabelValue("Host", hostname); if (offset != 0) { headers.addLabelValue(RANGE_HTTP_HEADER, HEADER_BYTES_PREFIX + String.valueOf(offset) + HEADER_BYTES_SUFFIX); } InetSocketAddress sockAddr = new InetSocketAddress(addr, port); Socket socket = new Socket(); socket.setSoTimeout(socketTimeoutMs); socket.setReceiveBufferSize(BUF_SIZE); socket.connect(sockAddr, connectTimeoutMs); OutputStream socketOut = socket.getOutputStream(); InputStream socketIn = socket.getInputStream(); socketOut.write(requestMessage.getBytes(true)); socketOut.write(headers.getUTF8Bytes()); socketOut.flush(); HttpResponse response = HttpResponse.load(socketIn); String contentType = response.getHeaders().asMap().get("Content-Type"); if (contentType == null) { contentType = "application/unknown"; } String xferEncoding = response.getHeaders().asMap().get("Transfer-Encoding"); if (xferEncoding != null) { if (xferEncoding.equals("chunked")) { socketIn = new ChunkedInputStream(socketIn); } } ds = new URLDataSource(socketIn, contentType); } else { // assume a local file path: File f = new File(location); if (f.isFile() && f.canRead()) { long size = f.length(); if (size < offset) { throw new IOException("short file " + location + " cannot" + " seek to offset " + offset); } RandomAccessFile raf = new RandomAccessFile(f, "r"); raf.seek(offset); // BUGBUG: is it compressed? ds = new FileDataSource(raf, DEFAULT_CONTENT_TYPE); } else { throw new IOException("No readable file at " + location); } } return ds; }