Example usage for java.net Socket connect

List of usage examples for java.net Socket connect

Introduction

In this page you can find the example usage for java.net Socket connect.

Prototype

public void connect(SocketAddress endpoint, int timeout) throws IOException 

Source Link

Document

Connects this socket to the server with a specified timeout value.

Usage

From source file:org.jboss.test.kerberos.gss.GSSTestServer.java

/**
 * Sends STOP ({@link #CMD_STOP}) command to a running server.
 *//* w  w w .  j  a v a2 s  . co m*/
private void stop() {
    System.out.println("Sending STOP command GSSTestServer.");
    // Create an unbound socket
    final Socket socket = new Socket();
    try {
        socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), PORT), SOCKET_TIMEOUT);
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeInt(CMD_STOP);
        dos.flush();
        System.out.println("STOP command sent.");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.eclipse.mylyn.internal.commons.repositories.http.core.PollingProtocolSocketFactory.java

public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {

    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null"); //$NON-NLS-1$
    }/*from ww  w.j a v  a2s .  com*/

    final Socket socket = sock != null ? sock : NetUtil.configureSocket(factory.createSocket());

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);

    socket.bind(localAddress);
    socket.connect(remoteAddress, connTimeout);
    return socket;
}

From source file:com.googlecode.jcimd.TcpNetConnectionFactory.java

@Override
public Connection getConnection() throws Exception {
    Socket socket = SocketFactory.getDefault().createSocket();
    if (logger.isDebugEnabled()) {
        logger.debug("Connecting to [" + host + ":" + port + "]...");
    }//from   w  w w  . j  av a  2s  .  c  o  m
    socket.connect(new InetSocketAddress(this.host, this.port), 2000);
    if (logger.isDebugEnabled()) {
        logger.debug("Connected to [" + host + ":" + port + "]");
    }
    if (this.timeout > 0) {
        socket.setSoTimeout(this.timeout);
    }
    PacketSerializer serializer = new PacketSerializer();
    serializer.setSequenceNumberGenerator(new ApplicationPacketSequenceNumberGenerator());
    TcpNetConnection newConnection = new TcpNetConnection(socket, serializer, this.username, this.password);
    this.executor.execute(newConnection);
    newConnection.login();
    return newConnection;
}

From source file:org.workin.fastdfs.factory.DefaultPoolableTrackerServerFactory.java

@Override
public void activateObject(TrackerServer trackerServer) throws Exception {
    Socket socket = trackerServer.getSocket();
    if (!socket.isConnected() || socket.isClosed()) {
        /* ?TrackerServer */
        socket.setSoTimeout(ClientGlobal.g_network_timeout);
        socket.connect(trackerServer.getInetSocketAddress(), ClientGlobal.g_connect_timeout);
    }/*  www .ja  va 2  s  .co  m*/
}

From source file:com.spotify.docker.client.UnixConnectionSocketFactory.java

@Override
public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host,
        final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context)
        throws IOException {
    try {//from   w  w w .j  ava2 s  .c  o  m
        socket.connect(new UnixSocketAddress(socketFile), connectTimeout);
    } catch (SocketTimeoutException e) {
        throw new ConnectTimeoutException(e, null, remoteAddress.getAddress());
    }

    return socket;
}

From source file:org.mule.transport.http.MuleSecureProtocolSocketFactory.java

/**
 * This is a direct version of code in {@link ReflectionSocketFactory}.
 *//*w w  w  .  ja v a 2s  .  c om*/
protected Socket createSocketWithTimeout(String host, int port, InetAddress localAddress, int localPort,
        int timeout) throws IOException {
    Socket socket = socketFactory.createSocket();
    SocketAddress local = new InetSocketAddress(localAddress, localPort);
    SocketAddress remote = new InetSocketAddress(host, port);

    socket.bind(local);
    socket.connect(remote, timeout);
    return socket;
}

From source file:ru.codemine.ccms.counters.kondor.KondorClient.java

private File ftpDownload(Shop shop) {
    String login = settingsService.getCountersKondorFtpLogin();
    String pass = settingsService.getCountersKondorFtpPassword();
    String ip = shop.getProvider().getIp();
    String tmpFileName = settingsService.getStorageRootPath()
            + DateTime.now().toString("YYYYMMdd-s" + shop.getId());
    try {//from www  .  j a va2s. c o m
        log.debug("Starting ftp session...");

        Socket manageSocket = new Socket();
        manageSocket.connect(new InetSocketAddress(ip, 21), 3000);
        BufferedReader in = new BufferedReader(new InputStreamReader(manageSocket.getInputStream()));
        PrintWriter out = new PrintWriter(manageSocket.getOutputStream(), true);
        String ftpAnswer;

        ftpAnswer = in.readLine();
        log.debug("FTP greetings: " + ftpAnswer);

        out.println("USER " + login);
        ftpAnswer = in.readLine();
        log.debug("FTP USER command responce: " + ftpAnswer);

        out.println("PASS " + pass);
        String afterAuth = in.readLine();
        log.debug("FTP PASS command responce: " + afterAuth);

        out.println("PASV");
        String pasvResponce = in.readLine();
        log.debug("FTP: PASV command responce: " + pasvResponce);

        if (pasvResponce.startsWith("227 (")) {
            pasvResponce = pasvResponce.substring(5);
            List<String> parsedPasv = new ArrayList<>(Arrays.asList(pasvResponce.split(",")));
            String p4 = parsedPasv.get(4);
            String p5 = parsedPasv.get(5).replace(")", "");
            int port = Integer.parseInt(p4) * 256 + Integer.parseInt(p5);

            log.debug("FTP: Recieved port: " + port);

            Socket dataSocket = new Socket();
            dataSocket.connect(new InetSocketAddress(ip, port), 3000);
            InputStream dataIn = dataSocket.getInputStream();
            FileOutputStream dataOut = new FileOutputStream(tmpFileName);

            out.println("RETR total.dbf");
            ftpAnswer = in.readLine();
            log.debug("FTP RETR command responce: " + ftpAnswer);

            IOUtils.copy(dataIn, dataOut);

            dataOut.flush();
            dataOut.close();
            dataSocket.close();
        } else {
            if (afterAuth.startsWith("530")) {
                log.warn(
                        "     ?, : "
                                + shop.getName());

            } else {
                log.warn(
                        " ?     PASV, : "
                                + shop.getName());
            }

        }

        out.println("QUIT");
        ftpAnswer = in.readLine();
        log.debug("FTP QUIT command responce: " + ftpAnswer);

        manageSocket.close();
    } catch (Exception e) {
        log.warn(
                "?   ? ?   "
                        + shop.getName() + ",  : " + e.getMessage());
    }

    return new File(tmpFileName);
}

From source file:org.openhab.binding.satel.internal.protocol.Ethm1Module.java

@Override
protected CommunicationChannel connect() {
    logger.info("Connecting to ETHM-1 module at {}:{}", this.host, this.port);

    try {//from   ww  w  .j  ava 2  s .c o m
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(this.host, this.port), this.getTimeout());
        logger.info("ETHM-1 module connected successfuly");

        if (StringUtils.isBlank(this.encryptionKey)) {
            return new TCPCommunicationChannel(socket);
        } else {
            return new EncryptedCommunicationChannel(socket, this.encryptionKey);
        }
    } catch (IOException e) {
        logger.error("IO error occurred during connecting socket", e);
    }

    return null;
}

From source file:org.sonatype.nexus.apachehttpclient.NexusSSLConnectionSocketFactory.java

@Override
public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host,
        final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context)
        throws IOException {
    checkNotNull(host);//  ww  w.j  a  va 2s  .  c o m
    checkNotNull(remoteAddress);
    final Socket sock = socket != null ? socket : createSocket(context);
    if (localAddress != null) {
        sock.bind(localAddress);
    }
    try {
        sock.connect(remoteAddress, connectTimeout);
    } catch (final IOException e) {
        Closeables.close(sock, true);
        throw e;
    }
    // Setup SSL layering if necessary
    if (sock instanceof SSLSocket) {
        final SSLSocket sslsock = (SSLSocket) sock;
        sslsock.startHandshake();
        verifyHostname(sslsock, host.getHostName());
        return sock;
    } else {
        return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context);
    }
}

From source file:com.serphacker.serposcope.scraper.http.extensions.ScrapClientPlainConnectionFactory.java

@Override
public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host,
        final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context)
        throws IOException, ConnectTimeoutException {

    Socket sock;
    if (socket != null) {
        sock = socket;/*from  w w w  . j  ava 2 s.c o  m*/
    } else {
        sock = createSocket(context);
    }
    if (localAddress != null) {
        sock.bind(localAddress);
    }
    try {
        sock.connect(remoteAddress, connectTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException(ex, host, remoteAddress.getAddress());
    }
    return sock;
}