List of usage examples for java.net Socket isBound
public boolean isBound()
From source file:gov.nasa.arc.spife.europa.clientside.EuropaServerManager.java
private int findPort(int startAt) { Socket temp = null; for (int testPort = startAt; testPort < startAt + 1000; ++testPort) { try {// www. j a va2 s . c om writeLog("[findPort] Testing port " + testPort); temp = new Socket(EuropaServerManagerConfig.LOCALHOST, testPort); if (temp.isBound()) writeLog("[findPort] Successfully bound port. Skipping " + testPort); else writeLog("[findPort] Port " + testPort + " is in use. Moving on."); } catch (IOException ioe) { if (ioe instanceof ConnectException) return testPort; } //you cannot get ye port catch (SecurityException se) { writeLog("[findPort] Security exception testing a port. Returning -1.", DynamicEuropaLogger.ERROR); return -1; } finally { try { if (temp != null) temp.close(); } catch (Exception e) { writeLog("[findPort] Failed to close a test-socket.", DynamicEuropaLogger.ERROR); } } } writeLog("[findPort] Exhausted 1000 possible ports. Returning -1.", DynamicEuropaLogger.ERROR); return -1; }
From source file:com.delphix.session.test.ServiceTest.java
/** * Scan for an unused port.// w ww. jav a2 s. co m */ private int portScan() { int localPort = (int) (Math.random() * 1000) + 62626; do { Socket socket = new Socket(); try { socket.setReuseAddress(true); socket.bind(new InetSocketAddress(localPort)); assertTrue(socket.isBound()); break; } catch (IOException e) { logger.infof(e, "failed to bind to port %d - try next", localPort); localPort++; } finally { try { socket.close(); } catch (IOException e) { fail("failed to close socket", e); } } } while (localPort < 65536); if (localPort >= 65536) { fail("failed to find unused port"); } logger.infof("unused local port %d found", localPort); return localPort; }
From source file:com.symbian.driver.core.controller.tasks.TEFTask.java
/** * @param aVisitor/*w w w .j a va 2 s .c o m*/ * @param aTestExecuteScript * @param lExecuteOnDevice * @param aTask * @return The last execption raised when running UCC. * @throws JStatException */ private boolean runUCC(List<String> aArgs, Task aTask) { boolean lReturn = true; Socket lUccSocket = null; DataOutputStream lSocketOut = null; DataInputStream lSocketIn = null; int lRunNumber = 0; int lUccPort = -1; String lUccAddress = null; IDeviceComms.ISymbianProcess lProcess = null; try { String[] lUccSplit = TDConfig.getInstance().getPreference(TDConfig.UCC_IP_ADDRESS).split(":"); lRunNumber = TDConfig.getInstance().getPreferenceInteger(TDConfig.RUN_NUMBER); lUccAddress = lUccSplit[0]; lUccPort = Integer.parseInt(lUccSplit[1]); } catch (ParseException lParseException) { LOGGER.log(Level.SEVERE, "Could not get configuration for UCC.", lParseException); iExceptions.put(lParseException, ESeverity.ERROR); lReturn = false; } catch (NumberFormatException lNumberFormatException) { LOGGER.log(Level.SEVERE, "Could not parse the port number for UCC.", lNumberFormatException); iExceptions.put(lNumberFormatException, ESeverity.ERROR); lReturn = false; } if (lUccAddress == null || lUccAddress.equals("") || lUccPort < 0) { iExceptions.put( new UnknownHostException("Please specify a valid UCC address for example 192.168.0.1:3000"), ESeverity.ERROR); return false; } // Run the test try { LOGGER.info("Running UCC with:\n\tAddress: " + lUccAddress + "\n\tUCC Port:" + lUccPort); lUccSocket = new Socket(lUccAddress, lUccPort); lSocketOut = new DataOutputStream(lUccSocket.getOutputStream()); lSocketIn = new DataInputStream(lUccSocket.getInputStream()); LOGGER.fine("Starting UCC while still polling"); lProcess = iDeviceProxy.createSymbianProcess(); if (lProcess != null) { // run and don't wait if (!lProcess.runCommand(TEST_EXECUTE, aArgs, aTask.getTimeout() * 1000, false)) { iExceptions.put(new Exception("Failed to run TEF for UCC."), ESeverity.ERROR); lReturn = false; } // Tell UCC that the test has started. LOGGER.fine("Writing to UCC socket: " + lRunNumber); lSocketOut.writeInt(lRunNumber); lSocketOut.flush(); int lUCCReply = lSocketIn.readInt(); LOGGER.fine("UCC Reply: " + lUCCReply); } } catch (UnknownHostException lUnknownHostException) { LOGGER.log(Level.SEVERE, "Could not find UCC host", lUnknownHostException); iExceptions.put(lUnknownHostException, ESeverity.ERROR); return false; } catch (IOException lIOException) { LOGGER.log(Level.SEVERE, "IO Exception during UCC testing: " + lIOException.getMessage() + (lUccSocket != null ? "\nUcc Socket Connected: " + lUccSocket.isConnected() + "\nUcc Socket InputShutdown: " + lUccSocket.isInputShutdown() + "\nUcc Socket OutputShutdown:" + lUccSocket.isOutputShutdown() + "\nUcc Socket Bound: " + lUccSocket.isBound() : "\nUcc Socket is NULL"), lIOException); iExceptions.put(lIOException, ESeverity.ERROR); return false; } finally { // Close UCC if (lSocketOut != null) { try { LOGGER.log(Level.FINE, "Closing Socket Out."); lUccSocket.shutdownInput(); lUccSocket.shutdownOutput(); lSocketOut.close(); } catch (IOException lIOException) { LOGGER.log(Level.SEVERE, "Could not close UCC Out socket.", lIOException); iExceptions.put(lIOException, ESeverity.ERROR); } } if (lSocketIn != null) { try { LOGGER.log(Level.FINE, "Closing Socket In."); lSocketIn.close(); } catch (IOException lIOException) { LOGGER.log(Level.SEVERE, "Could not close UCC In socket.", lIOException); iExceptions.put(lIOException, ESeverity.ERROR); } } if (lUccSocket != null) { try { LOGGER.log(Level.FINE, "Closing Socket UCC."); lUccSocket.close(); } catch (IOException lIOException) { LOGGER.log(Level.SEVERE, "Could not close UCC socket.", lIOException); iExceptions.put(lIOException, ESeverity.ERROR); } } if (!lUccSocket.isClosed()) { LOGGER.warning("Could not close the UCC sockets properly."); } lSocketOut = null; lSocketIn = null; lUccSocket = null; // Poll TEF Test if (!lProcess.join()) { iExceptions.put(new Exception("Coud not join UCC-TEF Process"), ESeverity.ERROR); lReturn = false; } } return lReturn; }