當前位置: 首頁>>代碼示例>>Java>>正文


Java SocketFactory.createSocket方法代碼示例

本文整理匯總了Java中javax.net.SocketFactory.createSocket方法的典型用法代碼示例。如果您正苦於以下問題:Java SocketFactory.createSocket方法的具體用法?Java SocketFactory.createSocket怎麽用?Java SocketFactory.createSocket使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.net.SocketFactory的用法示例。


在下文中一共展示了SocketFactory.createSocket方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import javax.net.SocketFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    try (Server server = new Server()) {
        new Thread(server).start();

        SocketFactory factory = SSLSocketFactory.getDefault();
        try (SSLSocket socket = (SSLSocket) factory.createSocket("localhost",
                server.getPort())) {
            socket.setSoTimeout(2000);
            System.out.println("Client established TCP connection");
            boolean failed = false;
            for (TestCase testCase : testCases) {
                try {
                    testCase.test(socket);
                    System.out.println("ERROR: no exception");
                    failed = true;
                } catch (IOException e) {
                    System.out.println("Failed as expected: " + e);
                }
            }
            if (failed) {
                throw new Exception("One or more tests failed");
            }
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:26,代碼來源:CloseSocket.java

示例2: createSocket

import javax.net.SocketFactory; //導入方法依賴的package包/類
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>
 * 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 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 {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    Socket socket = null;
    
    SocketFactory socketfactory = SSLSocketFactory.getDefault();
    if (timeout == 0) {
        socket = socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
    }
    verifyHostname((SSLSocket)socket);
    return socket;
}
 
開發者ID:jenkinsci,項目名稱:lib-commons-httpclient,代碼行數:50,代碼來源:StrictSSLProtocolSocketFactory.java

示例3: connectToDN

import javax.net.SocketFactory; //導入方法依賴的package包/類
/**
 * Connect to the given datanode's datantrasfer port, and return
 * the resulting IOStreamPair. This includes encryption wrapping, etc.
 */
public static IOStreamPair connectToDN(DatanodeInfo dn, int timeout,
                                       Configuration conf,
                                       SaslDataTransferClient saslClient,
                                       SocketFactory socketFactory,
                                       boolean connectToDnViaHostname,
                                       DataEncryptionKeyFactory dekFactory,
                                       Token<BlockTokenIdentifier> blockToken)
    throws IOException {

  boolean success = false;
  Socket sock = null;
  try {
    sock = socketFactory.createSocket();
    String dnAddr = dn.getXferAddr(connectToDnViaHostname);
    LOG.debug("Connecting to datanode {}", dnAddr);
    NetUtils.connect(sock, NetUtils.createSocketAddr(dnAddr), timeout);
    sock.setSoTimeout(timeout);

    OutputStream unbufOut = NetUtils.getOutputStream(sock);
    InputStream unbufIn = NetUtils.getInputStream(sock);
    IOStreamPair pair = saslClient.newSocketSend(sock, unbufOut,
        unbufIn, dekFactory, blockToken, dn);

    IOStreamPair result = new IOStreamPair(
        new DataInputStream(pair.in),
        new DataOutputStream(new BufferedOutputStream(pair.out,
            NuCypherExtUtilClient.getSmallBufferSize(conf)))
    );

    success = true;
    return result;
  } finally {
    if (!success) {
      IOUtils.closeSocket(sock);
    }
  }
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:42,代碼來源:NuCypherExtUtilClient.java

示例4: createDirectRawSocket

import javax.net.SocketFactory; //導入方法依賴的package包/類
private SocketConnector createDirectRawSocket(String host, int port, boolean secure, int timeout) throws IOException
{
    // Select a socket factory.
    SocketFactory factory = mSocketFactorySettings.selectSocketFactory(secure);

    // Let the socket factory create a socket.
    Socket socket = factory.createSocket();

    // The address to connect to.
    Address address = new Address(host, port);

    // Create an instance that will execute the task to connect to the server later.
    return new SocketConnector(socket, address, timeout)
            .setVerifyHostname(mVerifyHostname);
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:16,代碼來源:WebSocketFactory.java

示例5: createSocket

import javax.net.SocketFactory; //導入方法依賴的package包/類
/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <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 {
    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;
    }
}
 
開發者ID:jenkinsci,項目名稱:lib-commons-httpclient,代碼行數:45,代碼來源:AuthSSLProtocolSocketFactory.java

示例6: createSocket

import javax.net.SocketFactory; //導入方法依賴的package包/類
/**
 * Attempts to get a new socket connection to the given host within the
 * given time limit.
 * <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
 */
@Override
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;
	}
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:46,代碼來源:EasySSLProtocolSocketFactory.java

示例7: doInBackground

import javax.net.SocketFactory; //導入方法依賴的package包/類
@Override
protected Boolean doInBackground(Void... params) {
    try {
        // TODO: support self-defined ports instead of 443 only
        SocketFactory factory = SSLSocketFactory.getDefault();
        SSLSocket socket = (SSLSocket) factory.createSocket();
        socket.connect(new InetSocketAddress(oasp_url.substring(8), 443), CON_TIMEOUT);
        socket.startHandshake();
        Certificate[] certs = socket.getSession().getPeerCertificates();
        socket.close();
        if (certs == null)
            return false;

        for (Certificate cert : certs) {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA256");
            messageDigest.update(cert.getEncoded());
            oasp_url_certs.add(convertToHex(messageDigest.digest()));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
 
開發者ID:baidu,項目名稱:OASP,代碼行數:25,代碼來源:OASPVerify.java

示例8: connect

import javax.net.SocketFactory; //導入方法依賴的package包/類
@Override
public DTunnel connect() throws IOException {
    SocketFactory ssf = SSLSocketFactory.getDefault(); 
    SSLSocket socket = (SSLSocket) ssf.createSocket(this.getHost(), DTP_PORT);
    socket.startHandshake();
    return this.connect(socket);
}
 
開發者ID:atpc,項目名稱:dtp,代碼行數:8,代碼來源:DTPSecureConnector.java

示例9: createProxiedRawSocket

import javax.net.SocketFactory; //導入方法依賴的package包/類
private SocketConnector createProxiedRawSocket(
        String host, int port, boolean secure, int timeout) throws IOException
{
    // Determine the port number of the proxy server.
    // Especially, if getPort() returns -1, the value
    // is converted to 80 or 443.
    int proxyPort = determinePort(mProxySettings.getPort(), mProxySettings.isSecure());

    // Select a socket factory.
    SocketFactory socketFactory = mProxySettings.selectSocketFactory();

    // Let the socket factory create a socket.
    Socket socket = socketFactory.createSocket();

    // The address to connect to.
    Address address = new Address(mProxySettings.getHost(), proxyPort);

    // The delegatee for the handshake with the proxy.
    ProxyHandshaker handshaker = new ProxyHandshaker(socket, host, port, mProxySettings);

    // SSLSocketFactory for SSL handshake with the WebSocket endpoint.
    SSLSocketFactory sslSocketFactory = secure ?
            (SSLSocketFactory)mSocketFactorySettings.selectSocketFactory(secure) : null;

    // Create an instance that will execute the task to connect to the server later.
    return new SocketConnector(
            socket, address, timeout, handshaker, sslSocketFactory, host, port)
            .setVerifyHostname(mVerifyHostname);
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:30,代碼來源:WebSocketFactory.java


注:本文中的javax.net.SocketFactory.createSocket方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。