List of usage examples for java.net Socket close
public synchronized void close() throws IOException
From source file:dk.dma.ais.bus.provider.TcpClientProvider.java
@Override public void cancel() { // Close socket Socket s = socket; if (s != null) { try {// w w w.j a v a 2s .c o m s.close(); } catch (IOException ignore) { } } getThread().interrupt(); try { getThread().join(THREAD_STOP_WAIT_MAX); } catch (InterruptedException e) { e.printStackTrace(); } setStopped(); }
From source file:net.javacrumbs.mocksocket.SampleTest.java
@Test public void testMultiple() throws Exception { byte[] mockData1 = new byte[] { 1, 2, 3, 4 }; byte[] mockData2 = new byte[] { 1, 2, 3, 4 }; expectCall().andReturn(data(mockData1)).andReturn(data(mockData2)); Socket socket1 = SocketFactory.getDefault().createSocket("example.org", 1234); byte[] data1 = IOUtils.toByteArray(socket1.getInputStream()); socket1.close(); assertThat(data1, is(mockData1));/*www .ja v a 2 s . co m*/ Socket socket2 = SocketFactory.getDefault().createSocket("example.org", 1234); byte[] data2 = IOUtils.toByteArray(socket2.getInputStream()); socket2.close(); assertThat(data2, is(mockData2)); }
From source file:org.transdroid.util.IgnoreTlsSniSocketFactory.java
@Override @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public Socket createSocket(Socket plainSocket, String host, int port, boolean autoClose) throws IOException { if (autoClose) { // we don't need the plainSocket plainSocket.close(); }/* w w w. java2 s . c o m*/ SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory .getDefault(0); // For self-signed certificates use a custom trust manager sslSocketFactory.setTrustManagers(new TrustManager[] { new IgnoreSSLTrustManager() }); // create and connect SSL socket, but don't do hostname/certificate verification yet SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port); // enable TLSv1.1/1.2 if available ssl.setEnabledProtocols(ssl.getSupportedProtocols()); // set up SNI before the handshake if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { sslSocketFactory.setHostname(ssl, host); } else { try { java.lang.reflect.Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class); setHostnameMethod.invoke(ssl, host); } catch (Exception e) { throw new IOException("SNI not usable: " + e, e); } } return ssl; }
From source file:com.jilk.ros.rosbridge.implementation.ROSBridgeWebSocketClient.java
@Override public void closeBlocking() throws InterruptedException { super.closeBlocking(); try {/*from w w w. j a v a2s . c o m*/ Field channelField = this.getClass().getSuperclass().getDeclaredField("channel"); channelField.setAccessible(true); SocketChannel channel = (SocketChannel) channelField.get(this); if (channel != null && channel.isOpen()) { Socket socket = channel.socket(); if (socket != null) socket.close(); } } catch (Exception ex) { System.out.println("Exception in Websocket close hack."); ex.printStackTrace(); } }
From source file:org.transdroid.util.TlsSniSocketFactory.java
@Override @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public Socket createSocket(Socket plainSocket, String host, int port, boolean autoClose) throws IOException { if (autoClose) { // we don't need the plainSocket plainSocket.close(); }/* w w w . j ava 2 s . c o m*/ SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory .getDefault(0); // create and connect SSL socket, but don't do hostname/certificate verification yet SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port); // enable TLSv1.1/1.2 if available ssl.setEnabledProtocols(ssl.getSupportedProtocols()); // set up SNI before the handshake if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { sslSocketFactory.setHostname(ssl, host); } else { try { java.lang.reflect.Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class); setHostnameMethod.invoke(ssl, host); } catch (Exception e) { Log.d(TlsSniSocketFactory.class.getSimpleName(), "SNI not usable: " + e); } } // verify hostname and certificate SSLSession session = ssl.getSession(); if (!hostnameVerifier.verify(host, session)) { throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host); } return ssl; }
From source file:com.brienwheeler.lib.io.ReconnectingSocket.java
public void stop() { ConnectThread thread = connectThread.getAndSet(null); if (thread != null) { try {/*w ww. j a v a2s.c o m*/ thread.shutdown(); } catch (InterruptedException e) { log.warn("interrupted while shutting down", e); Thread.currentThread().interrupt(); } } Socket socket = this.socket.getAndSet(null); if (socket != null) { try { socket.close(); } catch (IOException e) { log.error("error closing socket", e); } } }
From source file:net.javacrumbs.mocksocket.SampleTest.java
@Test public void testConditionalData() throws Exception { byte[] dataToWrite = new byte[] { 5, 4, 3, 2 }; byte[] mockData = new byte[] { 1, 2, 3, 4 }; expectCall().andWhenRequest(data(is(dataToWrite))).thenReturn(data(mockData)); Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234); IOUtils.write(dataToWrite, socket.getOutputStream()); byte[] data = IOUtils.toByteArray(socket.getInputStream()); socket.close(); assertThat(data, is(mockData));//from w w w .ja va 2 s . co m }
From source file:com.flipkart.phantom.thrift.impl.proxy.SocketObjectFactory.java
/** * Interface method implementation. Closes the specified Socket instance * @see org.apache.commons.pool.PoolableObjectFactory#destroyObject(Object) *//*from w w w.ja va 2 s . c o m*/ public void destroyObject(Socket socket) throws Exception { LOGGER.info("Closing a socket for server : {} at port : {}", this.getThriftProxy().getThriftServer(), this.getThriftProxy().getThriftPort()); socket.close(); }
From source file:cnxchecker.Client.java
private void doit() { // Check we can connect to the server try {//w ww . j av a 2 s .com Socket s = new Socket(this.ia, this.port); s.close(); } catch (IOException e) { printAndExit("Failed to contact " + this.ia + " " + this.port); } // Start the workers for (int i = 0; i < this.workers.length; i++) { try { workers[i] = new Worker(i); workers[i].start(); } catch (Exception e) { System.out.println("[ERROR] worker#" + i + ": failed to start thread -> " + e.getMessage()); System.exit(1); } } try { Thread.sleep(this.duration); } catch (InterruptedException e) { // Ok } for (int i = 0; i < this.workers.length; i++) { workers[i].terminate(); } System.out.println("Exiting"); }
From source file:org.apache.axis2.transport.http.server.AxisHttpConnectionImpl.java
public void shutdown() throws IOException { Socket tmpsocket = this.socket; if (tmpsocket != null) { tmpsocket.close(); }/*from ww w . ja v a 2 s.co m*/ }