当前位置: 首页>>代码示例>>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;未经允许,请勿转载。