List of usage examples for java.net Socket connect
public void connect(SocketAddress endpoint, int timeout) throws IOException
From source file:eu.stratosphere.nephele.taskmanager.TaskManager.java
public 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); }/*from w w w .ja v a2 s . c o 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 determine own IP address from '" + 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:org.fabric3.binding.ftp.runtime.ExpiringSocketFactory.java
private Socket createSocket(InetSocketAddress socketAddress, InetSocketAddress localSocketAddress) throws IOException { Socket socket = new Socket(); if (localSocketAddress != null) { socket.bind(localSocketAddress); }//from w w w. jav a2 s. c o m socket.connect(socketAddress, connectTimeout); return socket; }
From source file:org.apache.hadoop.dfs.TestBlockReplacement.java
private boolean replaceBlock(Block block, DatanodeInfo source, DatanodeInfo sourceProxy, DatanodeInfo destination) throws IOException { Socket sock = new Socket(); sock.connect(NetUtils.createSocketAddr(sourceProxy.getName()), FSConstants.READ_TIMEOUT); sock.setKeepAlive(true);//from www .j av a2 s .c o m // sendRequest DataOutputStream out = new DataOutputStream(sock.getOutputStream()); out.writeShort(FSConstants.DATA_TRANSFER_VERSION); out.writeByte(FSConstants.OP_COPY_BLOCK); out.writeLong(block.getBlockId()); out.writeLong(block.getGenerationStamp()); Text.writeString(out, source.getStorageID()); destination.write(out); out.flush(); // receiveResponse DataInputStream reply = new DataInputStream(sock.getInputStream()); short status = reply.readShort(); if (status == FSConstants.OP_STATUS_SUCCESS) { return true; } return false; }
From source file:com.lithium.flow.vault.AgentVault.java
@Nullable private String readAgent() { String agentPort = store.getValue("vault.agent.port"); String agentPassword = store.getValue("vault.agent.password"); if (agentPort == null || agentPassword == null) { return null; }/* w w w. ja v a2 s.co m*/ try { int port = Integer.parseInt(agentPort); Socket socket = new Socket(); socket.connect(new InetSocketAddress("localhost", port), 5000); @SuppressWarnings("unchecked") Map<String, String> map = mapper.readValue(socket.getInputStream(), Map.class); Store agentStore = new MemoryStore(map); Vault agentVault = new SecureVault(Configs.empty(), agentStore); agentVault.unlock(agentPassword); return agentVault.getValue("password"); } catch (Exception e) { log.debug("failed to read from agent", e); return null; } }
From source file:com.vmware.aurora.vc.vcservice.TlsSocketFactory.java
@Override 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"); }/*from w w w . j av a2s . co m*/ int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); socket.bind(new InetSocketAddress(localAddress, localPort)); socket.connect(new InetSocketAddress(host, port), timeout); return socket; } }
From source file:org.rhq.plugins.netservices.PortNetServiceComponent.java
private boolean portReachable() { Socket socket = null; try {/*from w ww.j a va2s . c o m*/ socket = new Socket(); socket.connect( new InetSocketAddress(componentConfiguration.getAddress(), componentConfiguration.getPort()), CONNECTION_TIMEOUT); return true; } catch (Exception e) { if (LOG.isTraceEnabled()) { LOG.trace("Unable to reach remote port: " + componentConfiguration.toString(), e); } } finally { if (socket != null) { closeQuietly(socket); } } return false; }
From source file:org.apache.hadoop.hdfs.server.datanode.TestBlockReplacement.java
private boolean replaceBlock(Block block, DatanodeInfo source, DatanodeInfo sourceProxy, DatanodeInfo destination) throws IOException { Socket sock = new Socket(); sock.connect(NetUtils.createSocketAddr(destination.getName()), HdfsConstants.READ_TIMEOUT); sock.setKeepAlive(true);/* ww w.j a v a 2s . c o m*/ // sendRequest DataOutputStream out = new DataOutputStream(sock.getOutputStream()); out.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION); out.writeByte(DataTransferProtocol.OP_REPLACE_BLOCK); out.writeLong(block.getBlockId()); out.writeLong(block.getGenerationStamp()); Text.writeString(out, source.getStorageID()); sourceProxy.write(out); BlockTokenSecretManager.DUMMY_TOKEN.write(out); out.flush(); // receiveResponse DataInputStream reply = new DataInputStream(sock.getInputStream()); short status = reply.readShort(); if (status == DataTransferProtocol.OP_STATUS_SUCCESS) { return true; } return false; }
From source file:com.collabnet.svnedge.net.SslProtocolSocketFactory.java
public Socket createSocket(String remoteHost, int remotePort, InetAddress clientHost, int clientPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); }/*from ww w . j a v a 2 s . c om*/ int timeout = params.getConnectionTimeout(); if (timeout == 0) { return getSocketFactory().createSocket(remoteHost, remotePort, clientHost, clientPort); } else { Socket socket = getSocketFactory().createSocket(); socket.bind(new InetSocketAddress(clientHost, clientPort)); socket.connect(new InetSocketAddress(remoteHost, remotePort), timeout); return socket; } }
From source file:it.evilsocket.dsploit.core.System.java
public static boolean isPortAvailable(int port) { boolean available = true; int available_code = mOpenPorts.get(port); if (available_code != 0) return available_code == 1 ? false : true; try {/*w ww .j a v a2s. co m*/ // attempt 3 times since proxy and server could be still releasing // their ports for (int i = 0; i < 3; i++) { Socket channel = new Socket(); InetSocketAddress address = new InetSocketAddress( InetAddress.getByName(mNetwork.getLocalAddressAsString()), port); channel.connect(address, 200); available = !channel.isConnected(); channel.close(); if (available) break; Thread.sleep(200); } } catch (Exception e) { available = true; } mOpenPorts.put(port, available ? 2 : 1); return available; }
From source file:ch.algotrader.adapter.ib.IBSession.java
private void waitAndConnect() { while (!isTerminated()) { Socket socket = new Socket(); try {//from w w w . ja v a 2 s . co m socket.connect(new InetSocketAddress(this.host, this.port), 5000); eConnect(socket, this.clientId); return; } catch (ConnectException e) { // do nothing, gateway is down if (LOGGER.isWarnEnabled()) { LOGGER.warn("please start IB Gateway / TWS on port: {}", this.port); } } catch (IOException e) { LOGGER.error("connection error", e); } try { socket.close(); } catch (IOException ignore) { } sleep(); } }