Java Code Examples for javax.net.SocketFactory#createSocket()
The following examples show how to use
javax.net.SocketFactory#createSocket() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCPNetSyslogWriter.java From syslog4j with GNU Lesser General Public License v2.1 | 6 votes |
protected Socket createSocket(InetAddress hostAddress, int port, boolean keepalive) throws IOException { SocketFactory socketFactory = obtainSocketFactory(); Socket newSocket = socketFactory.createSocket(hostAddress,port); if (this.tcpNetSyslogConfig.isSoLinger()) { newSocket.setSoLinger(true,this.tcpNetSyslogConfig.getSoLingerSeconds()); } if (this.tcpNetSyslogConfig.isKeepAlive()) { newSocket.setKeepAlive(keepalive); } if (this.tcpNetSyslogConfig.isReuseAddress()) { newSocket.setReuseAddress(true); } return newSocket; }
Example 2
Source File: AuthSSLProtocolSocketFactory.java From http4e with Apache License 2.0 | 6 votes |
/** * 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; } }
Example 3
Source File: SSLSocketFactory.java From elasticsearch-hadoop with Apache License 2.0 | 6 votes |
@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"); } 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; } }
Example 4
Source File: RemoteSPADEQueryConnection.java From SPADE with GNU General Public License v3.0 | 6 votes |
public synchronized void connect(final SocketFactory socketFactory, final int timeoutInMillis) throws Exception{ mustNotBeConnected(); if(socketFactory == null){ throw new RuntimeException("NULL socket factory"); } Socket socket = null; try{ socket = socketFactory.createSocket(); _connect(socket, timeoutInMillis); }catch(Exception e){ if(socket != null){ try{ if(!socket.isClosed()){ socket.close(); } }catch(Exception e0){} } throw e; } }
Example 5
Source File: AbstractHdfsConnector.java From pulsar with Apache License 2.0 | 6 votes |
protected void checkHdfsUriForTimeout(Configuration config) throws IOException { URI hdfsUri = FileSystem.getDefaultUri(config); String address = hdfsUri.getAuthority(); int port = hdfsUri.getPort(); if (address == null || address.isEmpty() || port < 0) { return; } InetSocketAddress namenode = NetUtils.createSocketAddr(address, port); SocketFactory socketFactory = NetUtils.getDefaultSocketFactory(config); Socket socket = null; try { socket = socketFactory.createSocket(); NetUtils.connect(socket, namenode, 1000); // 1 second timeout } finally { IOUtils.closeQuietly(socket); } }
Example 6
Source File: SocketFactoryTest.java From j2objc with Apache License 2.0 | 6 votes |
public void test_createSocket_InetAddressIInetAddressI_ExceptionOrder() throws IOException { int invalidPort = Integer.MAX_VALUE; SocketFactory sf = SocketFactory.getDefault(); int validServerPortNumber = new ServerSocket(0).getLocalPort(); // Create a socket with localhost as the client address so that another attempt to bind // would fail. Socket s = sf.createSocket(InetAddress.getLocalHost() /* ServerAddress */, validServerPortNumber /* ServerPortNumber */, InetAddress.getLocalHost() /* ClientAddress */, 0 /* ClientPortNumber */); int assignedLocalPortNumber = s.getLocalPort(); // Create a socket with an invalid port and localhost as the client address. Both // BindException and IllegalArgumentException are expected in this case as the address is // already bound to the socket above and port is invalid, however, to preserve the // precedence order, IllegalArgumentException should be thrown. try (Socket s1 = sf.createSocket(InetAddress.getLocalHost() /* ServerAddress */, invalidPort /* ServerPortNumber */, InetAddress.getLocalHost() /* ClientAddress */, assignedLocalPortNumber /* ClientPortNumber */)) { fail("IllegalArgumentException wasn't thrown for " + invalidPort); } catch (IllegalArgumentException expected) { } }
Example 7
Source File: CloseSocket.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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"); } } } }
Example 8
Source File: AbstractHdfsConnector.java From pulsar with Apache License 2.0 | 6 votes |
protected void checkHdfsUriForTimeout(Configuration config) throws IOException { URI hdfsUri = FileSystem.getDefaultUri(config); String address = hdfsUri.getAuthority(); int port = hdfsUri.getPort(); if (address == null || address.isEmpty() || port < 0) { return; } InetSocketAddress namenode = NetUtils.createSocketAddr(address, port); SocketFactory socketFactory = NetUtils.getDefaultSocketFactory(config); Socket socket = null; try { socket = socketFactory.createSocket(); NetUtils.connect(socket, namenode, 1000); // 1 second timeout } finally { IOUtils.closeQuietly(socket); } }
Example 9
Source File: AbstractHadoopProcessor.java From nifi with Apache License 2.0 | 6 votes |
protected void checkHdfsUriForTimeout(Configuration config) throws IOException { URI hdfsUri = FileSystem.getDefaultUri(config); String address = hdfsUri.getAuthority(); int port = hdfsUri.getPort(); if (address == null || address.isEmpty() || port < 0) { return; } InetSocketAddress namenode = NetUtils.createSocketAddr(address, port); SocketFactory socketFactory = NetUtils.getDefaultSocketFactory(config); Socket socket = null; try { socket = socketFactory.createSocket(); NetUtils.connect(socket, namenode, 1000); // 1 second timeout } finally { IOUtils.closeQuietly(socket); } }
Example 10
Source File: AbstractHadoopProcessor.java From localization_nifi with Apache License 2.0 | 6 votes |
protected void checkHdfsUriForTimeout(Configuration config) throws IOException { URI hdfsUri = FileSystem.getDefaultUri(config); String address = hdfsUri.getAuthority(); int port = hdfsUri.getPort(); if (address == null || address.isEmpty() || port < 0) { return; } InetSocketAddress namenode = NetUtils.createSocketAddr(address, port); SocketFactory socketFactory = NetUtils.getDefaultSocketFactory(config); Socket socket = null; try { socket = socketFactory.createSocket(); NetUtils.connect(socket, namenode, 1000); // 1 second timeout } finally { IOUtils.closeQuietly(socket); } }
Example 11
Source File: EasySSLProtocolSocketFactory.java From http4e with Apache License 2.0 | 6 votes |
/** * 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; } }
Example 12
Source File: CloseSocket.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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"); } } } }
Example 13
Source File: CustomSSLProtocolSocketFactory.java From developer-studio with Apache License 2.0 | 6 votes |
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = ctx.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; } }
Example 14
Source File: CloseSocket.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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"); } } } }
Example 15
Source File: SSLGuacamoleSocket.java From guacamole-client with Apache License 2.0 | 5 votes |
/** * Creates a new SSLGuacamoleSocket which reads and writes instructions * to the Guacamole instruction stream of the Guacamole proxy server * running at the given hostname and port using SSL. * * @param hostname The hostname of the Guacamole proxy server to connect to. * @param port The port of the Guacamole proxy server to connect to. * @throws GuacamoleException If an error occurs while connecting to the * Guacamole proxy server. */ public SSLGuacamoleSocket(String hostname, int port) throws GuacamoleException { // Get factory for SSL sockets SocketFactory socket_factory = SSLSocketFactory.getDefault(); try { logger.debug("Connecting to guacd at {}:{} via SSL/TLS.", hostname, port); // Get address SocketAddress address = new InetSocketAddress( InetAddress.getByName(hostname), port ); // Connect with timeout sock = socket_factory.createSocket(); sock.connect(address, SOCKET_TIMEOUT); // Set read timeout sock.setSoTimeout(SOCKET_TIMEOUT); // On successful connect, retrieve I/O streams reader = new ReaderGuacamoleReader(new InputStreamReader(sock.getInputStream(), "UTF-8")); writer = new WriterGuacamoleWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8")); } catch (IOException e) { throw new GuacamoleServerException(e); } }
Example 16
Source File: WebSocketClient.java From java-android-websocket-client with Apache License 2.0 | 5 votes |
/** * Creates and connects a TCP socket for the underlying connection * * @return true is the socket was successfully connected, false * otherwise * @throws IOException */ private boolean createAndConnectTCPSocket() throws IOException { synchronized (internalLock) { if (!isClosed) { String scheme = uri.getScheme(); int port = uri.getPort(); if (scheme != null) { if (scheme.equals("ws")) { SocketFactory socketFactory = SocketFactory.getDefault(); socket = socketFactory.createSocket(); socket.setSoTimeout(readTimeout); if (port != -1) { socket.connect(new InetSocketAddress(uri.getHost(), port), connectTimeout); } else { socket.connect(new InetSocketAddress(uri.getHost(), 80), connectTimeout); } } else if (scheme.equals("wss")) { socket = socketFactory.createSocket(); socket.setSoTimeout(readTimeout); if (port != -1) { socket.connect(new InetSocketAddress(uri.getHost(), port), connectTimeout); } else { socket.connect(new InetSocketAddress(uri.getHost(), 443), connectTimeout); } } else { throw new IllegalSchemeException("The scheme component of the URI should be ws or wss"); } } else { throw new IllegalSchemeException("The scheme component of the URI cannot be null"); } return true; } return false; } }
Example 17
Source File: WebSocketFactory.java From iGap-Android with GNU Affero General Public License v3.0 | 5 votes |
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); }
Example 18
Source File: SSLChannel.java From yajsync with GNU General Public License v3.0 | 5 votes |
public static SSLChannel open(String address, int port, int contimeout, int timeout) throws IOException { SocketFactory factory = SSLSocketFactory.getDefault(); InetSocketAddress socketAddress = new InetSocketAddress(address, port); Socket sock = factory.createSocket(); sock.connect(socketAddress, contimeout); return new SSLChannel((SSLSocket) sock, timeout); }
Example 19
Source File: DeepStubbingTest.java From astor with GNU General Public License v2.0 | 4 votes |
@Test public void myTest() throws Exception { SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS); when(sf.createSocket(anyString(), eq(80))).thenReturn(null); sf.createSocket("what", 80); }
Example 20
Source File: AbstractSocketTransport.java From mongodb-async-driver with Apache License 2.0 | 4 votes |
/** * Tries to open a connection to the server. * * @param server * The server to open the connection to. * @param config * The configuration for attempting to open the connection. * @return The opened {@link Socket}. * @throws IOException * On a failure opening a connection to the server. */ private Socket openSocket(final Server server, final MongoClientConfiguration config) throws IOException { final SocketFactory factory = config.getSocketFactory(); IOException last = null; Socket socket = null; for (final InetSocketAddress address : myServer.getAddresses()) { try { socket = factory.createSocket(); socket.connect(address, config.getConnectTimeout()); // If the factory wants to know about the connection then let it // know first. if (factory instanceof SocketConnectionListener) { ((SocketConnectionListener) factory).connected(address, socket); } // Let the server know the working connection. server.connectionOpened(address); last = null; break; } catch (final IOException error) { last = error; try { if (socket != null) { socket.close(); } } catch (final IOException ignore) { myLog.info( "Could not close the defunct socket connection: {}", socket); } } } if (last != null) { server.connectFailed(); throw last; } return socket; }