List of usage examples for java.net Socket connect
public void connect(SocketAddress endpoint, int timeout) throws IOException
From source file:fr.treeptik.cloudunit.utils.PortUtils.java
private boolean isPortOpened(String ip, Integer port) { try {// ww w .j a v a 2 s .c om Socket socket = new Socket(); socket.connect(new InetSocketAddress(ip, port), 500); socket.close(); return true; } catch (Exception ex) { return false; } }
From source file:org.jmxtrans.embedded.util.pool.SocketWriterPoolFactory.java
@Override public SocketWriter create(HostAndPort hostAndPort) throws Exception { Socket socket = new Socket(); socket.setKeepAlive(true);/* w w w . ja v a 2 s.com*/ socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()), socketConnectTimeoutInMillis); return new SocketWriter(socket, charset); }
From source file:hudson.util.NoClientBindProtocolSocketFactory.java
/** * Attempts to get a new socket connection to the given host within the given time limit. * <p>// w ww . ja v a2 s .com * This method employs several techniques to circumvent the limitations of older JREs that * do not support connect timeout. When running in JRE 1.4 or above reflection is used to * call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older * JREs 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 localAddress the local host name/IP to bind the socket to, ignored * @param localPort the port on the local machine, ignored * @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 * @throws ConnectTimeoutException if socket cannot be connected within the * given time limit * * @since 3.0 */ public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); if (timeout == 0) { // ignore the local address/port for binding return createSocket(host, port); } else { Socket s = new Socket(); s.connect(new InetSocketAddress(host, port), timeout); return s; } }
From source file:org.punksearch.logic.online.Probe.java
/** * Checks if custom port is open at the host * * @param ip IP of the host to check//from w w w. j a va2 s . c o m * @param port Port of the host to check * @return true if port is open, false if closed */ public boolean connect(String ip, int port) { boolean result; try { SocketAddress sockaddr = new InetSocketAddress(ip, port); Socket s = new Socket(); s.connect(sockaddr, PROBE_TIMEOUT); s.close(); result = true; } catch (IOException e) { log.trace(ip + ":" + port + " -> " + e); result = false; } log.debug("Probe (connect): " + ip + ":" + port + " = " + result); return result; }
From source file:org.openhab.binding.neohub.internal.NeoHubConnector.java
/** * Sends the message over the network and provides the response to the * response handler.//from w w w .j a va2 s.c o m * * @param msg * message to neohub * @param handler * handler to process the response, may be <code>null</code> * @return result of handler or <code>null</code> if network problem * occurred */ public <T> T sendMessage(final String msg, final ResponseHandler<T> handler) { final StringBuilder response = new StringBuilder(); final Socket socket = new Socket(); try { socket.connect(new InetSocketAddress(hostname, port), timeout); final InputStreamReader in = new InputStreamReader(socket.getInputStream(), US_ASCII); final OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream(), US_ASCII); logger.debug(">> {}", msg); out.write(msg); out.write(0); // NUL terminate the command string out.flush(); int l; while ((l = in.read()) > 0) {// NUL termination & end of stream (-1) response.append((char) l); } } catch (final IOException e) { logger.error("Failed to connect to neohub [host '{}' port '{}' timeout '{}']", new Object[] { hostname, port, timeout }); logger.debug("Failed to connect to neohub.", e); return null; } finally { IOUtils.closeQuietly(socket); } final String responseStr = response.toString(); logger.debug("<< {}", responseStr); if (handler != null) { return handler.onResponse(responseStr); } else return null; }
From source file:com.zimbra.common.net.ProtocolSocketFactoryWrapper.java
@Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException { int timeout = params != null ? params.getConnectionTimeout() : 0; if (timeout > 0) { Socket sock = factory.createSocket(); sock.bind(new InetSocketAddress(localAddress, localPort)); sock.connect(new InetSocketAddress(host, port), timeout); return sock; } else {// ww w . j a v a 2s .c o m return factory.createSocket(host, port, localAddress, localPort); } }
From source file:io.fabric8.kit.build.service.docker.access.hc.util.AbstractNativeSocketFactory.java
@Override public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException { sock.connect(createSocketAddress(path), connectTimeout); return sock;//from w w w. jav a 2 s . co m }
From source file:org.apache.jmeter.visualizers.backend.graphite.SocketOutputStreamPoolFactory.java
@Override public SocketOutputStream create(SocketConnectionInfos connectionInfos) throws Exception { Socket socket = new Socket(); socket.setKeepAlive(true);/*from w ww. j a v a 2 s . c o m*/ socket.setSoTimeout(socketTimeoutInMillis); socket.connect(new InetSocketAddress(connectionInfos.getHost(), connectionInfos.getPort()), socketConnectTimeoutInMillis); return new SocketOutputStream(socket); }
From source file:com.cloud.network.bigswitch.TrustingProtocolSocketFactory.java
@Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { int timeout = params.getConnectionTimeout(); if (timeout == 0) { return createSocket(host, port, localAddress, localPort); } else {/*from w w w . j a va2 s . c om*/ Socket s = ssf.createSocket(); s.bind(new InetSocketAddress(localAddress, localPort)); s.connect(new InetSocketAddress(host, port), timeout); return s; } }
From source file:hudson.plugins.chainreactorclient.ChainReactorInvalidServerException.java
public boolean connect(ChainReactorServer server) { try {/*from ww w . j av a 2 s .c o m*/ InetSocketAddress sockaddr = server.getSocketAddress(); Socket sock = new Socket(); sock.setSoTimeout(2000); sock.connect(sockaddr, 2000); BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream())); BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); ensureValidServer(rd); logger.println("Connected to chain reactor server"); sendBuildInfo(wr); rd.close(); wr.close(); sock.close(); } catch (UnknownHostException uhe) { logError("Failed to resolve host: " + server.getUrl()); } catch (SocketTimeoutException ste) { logError("Time out while trying to connect to " + server.toString()); } catch (IOException ioe) { logError(ioe.getMessage()); } catch (ChainReactorInvalidServerException crise) { logError(crise.getMessage()); } return true; }