List of usage examples for java.net Socket close
public synchronized void close() throws IOException
From source file:com.bmwcarit.barefoot.matcher.ServerTest.java
private void sendRequest(InetAddress host, int port, JSONArray samples) throws InterruptedException, IOException, JSONException { int trials = 120; int timeout = 500; Socket client = null; while (client == null || !client.isConnected()) { try {// ww w . j ava 2 s.c o m client = new Socket(host, port); } catch (IOException e) { Thread.sleep(timeout); if (trials == 0) { client.close(); throw new IOException(e.getMessage()); } else { trials -= 1; } } } PrintWriter writer = new PrintWriter(client.getOutputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); writer.println(samples.toString()); writer.flush(); String code = reader.readLine(); assertEquals("SUCCESS", code); String response = reader.readLine(); client.close(); MatcherKState state = new MatcherKState(new JSONObject(response), new MatcherFactory(ServerControl.getServer().getMap())); OutputFormatter output = new GeoJSONOutputFormatter(); PrintWriter out = new PrintWriter(ServerTest.class.getResource("").getPath() + "ServerTest-matching.json"); out.println(output.format(null, state)); out.close(); assertEquals(samples.length(), state.sequence().size()); }
From source file:com.seyren.core.service.notification.IrcCatNotificationService.java
private void sendMessage(String ircCatHost, int ircCatPort, String message, String channel) throws IOException { Socket socket = new Socket(ircCatHost, ircCatPort); Closer closer = Closer.create();//from ww w .j av a 2s.c om try { Writer out = closer.register(new OutputStreamWriter(socket.getOutputStream())); out.write(format("%s %s\n", channel, message)); out.flush(); } catch (IOException e) { socket.close(); throw closer.rethrow(e); } finally { closer.close(); } }
From source file:com.dbay.apns4j.impl.ApnsConnectionImpl.java
private void closeSocket(Socket socket) { try {//from w w w. j ava2s .c o m if (socket != null) { socket.close(); } } catch (Exception e) { logger.error(e.getMessage(), e); } }
From source file:edu.umd.cs.marmoset.utilities.UptimeDaemon.java
public void run() { // Listen on a socket for connections and report the current uptime Thread t = new Thread() { @Override//from w w w . j ava2 s . c om public void run() { while (true) { try { Socket sock = serverSocket.accept(); String uptime = shellCommand("uptime"); sock.getOutputStream().write(uptime.getBytes()); sock.close(); } catch (IOException e) { System.out.println(e); e.printStackTrace(); } catch (InterruptedException e) { System.out.println(e); e.printStackTrace(); } } } }; t.start(); //if (false) new Thread() { @Override public void run() { while (true) { try { String uptime = shellCommand("uptime"); //System.out.println(uptime); processLoad(uptime); Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); }
From source file:org.andstatus.app.net.http.TlsSniSocketFactory.java
@Override public Socket connectSocket(int timeout, Socket plain, HttpHost host, InetSocketAddress remoteAddr, InetSocketAddress localAddr, HttpContext context) throws IOException { MyLog.d(this, "Preparing direct SSL connection (without proxy) to " + host); // we'll rather use an SSLSocket directly plain.close(); // create a plain SSL socket, but don't do hostname/certificate verification yet SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(remoteAddr.getAddress(), host.getPort()); // connect, set SNI, shake hands, verify, print connection info connectWithSNI(ssl, host.getHostName()); return ssl;//from w w w .j a v a 2s . c o m }
From source file:hudson.gridmaven.gridlayer.PluginImpl.java
/** * Compute the host name that Hadoop nodes can be used to talk to Name node. * * <p>//from w w w.j av a 2 s. c o m * We prefer to use {@link Hudson#getRootUrl()}, except we have to watch out for a possibility * that it points to a front end (like apache, router with port-forwarding, etc.), and if that is the case, * use some heuristics to find out a usable host name. * * TODO: move this to {@code Hudson.toComputer().getHostName()}. */ String getMasterHostName() throws IOException, InterruptedException { // check if rootURL is reliable Hudson h = Hudson.getInstance(); String rootUrl = h.getRootUrl(); if (rootUrl == null) { // the only option is to auto-detect. String real = h.toComputer().getHostName(); LOGGER.fine("Hudson root URL isn't configured. Using " + real + " instead"); return real; } // according to Hudson's setting, this is the host name that we can use to connect to master, // at least for HTTP. See if we can connect to the arbitrary port in this way. final String hostName = new URL(rootUrl).getHost(); final ServerSocket ss = new ServerSocket(0); Thread t = new Thread() { @Override public void run() { try { ss.accept(); } catch (IOException e) { // shouldn't happen LOGGER.log(Level.INFO, "Failed to accept", e); } finally { try { ss.close(); } catch (IOException e) { // ignore } } } }; t.start(); try { Socket s = new Socket(); s.connect(new InetSocketAddress(hostName, ss.getLocalPort()), 1000); s.close(); // yep, it worked return hostName; } catch (IOException e) { // no it didn't String real = h.toComputer().getHostName(); LOGGER.fine("Hudson root URL " + rootUrl + " looks like a front end. Using " + real + " instead"); return real; } }
From source file:org.apache.servicemix.http.ProviderEndpointTest.java
public void testSendProblemWithServerDying() throws Exception { HttpComponent http = new HttpComponent(); HttpSoapProviderEndpoint ep1 = new HttpSoapProviderEndpoint(); ep1.setService(new QName("http://servicemix.apache.org/samples/wsdl-first", "PersonService")); ep1.setEndpoint("soap"); ep1.setWsdl(new ClassPathResource("person.wsdl")); ep1.setValidateWsdl(false); // TODO: Soap 1.2 not handled yet ep1.setUseJbiWrapper(true);/*from w ww. j a v a 2 s.c o m*/ http.addEndpoint(ep1); container.activateComponent(http, "http"); container.start(); new Thread() { public void run() { ServerSocket ss = null; try { ss = new ServerSocket(8192); Socket s = ss.accept(); Thread.sleep(50); s.close(); } catch (Throwable t) { t.printStackTrace(); } finally { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); ServiceMixClient client = new DefaultServiceMixClient(container); InOut me = client.createInOutExchange(); me.setService(new QName("http://servicemix.apache.org/samples/wsdl-first", "PersonService")); me.setOperation(new QName("http://servicemix.apache.org/samples/wsdl-first", "GetPerson")); me.getInMessage().setContent( new StringSource("<jbi:message xmlns:jbi=\"http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper\"" + " xmlns:msg=\"http://servicemix.apache.org/samples/wsdl-first/types\" " + " name=\"Hello\" " + " type=\"msg:HelloRequest\" " + " version=\"1.0\">" + " <jbi:part>" + " <msg:GetPerson><msg:personId>id</msg:personId></msg:GetPerson>" + " </jbi:part>" + "</jbi:message>")); client.sendSync(me); assertEquals(ExchangeStatus.ERROR, me.getStatus()); }
From source file:greensopinion.restexample.test.web.WebApplicationContainer.java
private boolean testAvailablePort(int port) { try {//ww w . ja v a 2 s . c o m Socket socket = SocketFactory.getDefault().createSocket(); try { socket.bind(new InetSocketAddress("localhost", port)); return true; } catch (IOException e) { return false; } finally { socket.close(); } } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:at.bitfire.davdroid.mirakel.webdav.TlsSniSocketFactory.java
@Override public Socket connectSocket(int timeout, Socket plain, HttpHost host, InetSocketAddress remoteAddr, InetSocketAddress localAddr, HttpContext context) throws IOException { Log.d(TAG, "Preparing direct SSL connection (without proxy) to " + host); // we'll rather use an SSLSocket directly plain.close(); // create a plain SSL socket, but don't do hostname/certificate verification yet SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(remoteAddr.getAddress(), host.getPort()); setReasonableEncryption(ssl);/*from ww w .j av a 2 s . c o m*/ // connect, set SNI, shake hands, verify, print connection info connectWithSNI(ssl, host.getHostName()); return ssl; }
From source file:org.psit.transwatcher.TransWatcher.java
private void startConnectionKeepAliveWatchDog(final Socket newImageListenerSocket) { watchDogThread = new Thread(new Runnable() { public void run() { try { while (true) { Thread.sleep(5000); InetSocketAddress addr = new InetSocketAddress(newImageListenerSocket.getInetAddress(), 80); Socket s = new Socket(); s.connect(addr, 1000); s.close(); notifyMessage("WatchDog ping."); }//from w w w. jav a 2 s .co m } catch (InterruptedException e) { notifyMessage("WatchDog interrupted"); } catch (IOException e) { notifyMessage("WatchDog: Connection to card lost."); try { newImageListenerSocket.close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } }; }, "ConnectionWatchDog"); watchDogThread.start(); }