public static int findRandomAvailablePortOnAllLocalInterfaces() throws IOException { int port; try (ServerSocket socket = new ServerSocket(0);) { port = socket.getLocalPort(); socket.close(); } return port; }
@VisibleForTesting boolean canBind(int portNum) { try { new ServerSocket(portNum).close(); return true; } catch (SocketException se) { return false; } catch (IOException e) { throw new RuntimeException(e); } }
String msg_received; ServerSocket socket = new ServerSocket(1755); Socket clientSocket = socket.accept(); //This is blocking. It will wait. DataInputStream DIS = new DataInputStream(clientSocket.getInputStream()); msg_received = DIS.readUTF(); clientSocket.close(); socket.close();
if (!server.equals("commandline")) { try { ServerSocket serverSocket = new ServerSocket(localPort); serverSocket.close(); } catch (IOException e) { System.err.println("\nPort " + localPort + " already in use!"); throw e; System.err.println(" started in " + (System.currentTimeMillis() - startTime) / 1000. + " s on port " + localPort);
public class MyServer { public static final int PORT = 12345; public static void main(String[] args) throws IOException, InterruptedException { ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(PORT); Socket s = ss.accept(); Thread.sleep(5000); ss.close(); s.close(); } } public class MyClient { public static void main(String[] args) throws IOException, InterruptedException { Socket s = SocketFactory.getDefault().createSocket("localhost", MyServer.PORT); System.out.println(" connected: " + s.isConnected()); Thread.sleep(10000); System.out.println(" connected: " + s.isConnected()); } }
private void listPassif() throws UnknownHostException, IOException { myOut("150 Opening ASCII mode data"); final ServerSocket ss = new ServerSocket(port); final Socket incoming = ss.accept(); list(incoming); ss.close(); }
Socket socket = null; try { socket = serverSocket.accept(); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); if (buffer != null && buffer.length() > 0) { sendCachedEvents(oos); serverSocket.close(); } catch(InterruptedIOException e) { Thread.currentThread().interrupt();
@Override public void run() { ServerSocket server = null; Socket client = null; try { InetAddress bindAddress = InetAddress.getByName(address); int backlog = (behavior == Behavior.THROWING_CONNECT_TIMEOUTS) ? 1 : -1; // default server = new ServerSocket(port, backlog, bindAddress); if (behavior == Behavior.THROWING_CONNECT_TIMEOUTS) { // fill backlog queue client = new Socket(); client.connect(server.getLocalSocketAddress()); } TimeUnit.MINUTES.sleep(10); fail("Mock host wasn't expected to live more than 10 minutes"); } catch (IOException e) { fail("Unexpected I/O exception", e); } catch (InterruptedException e) { // interruption is the expected way to stop this runnable, exit try { if (client != null) client.close(); server.close(); } catch (IOException e1) { fail("Unexpected error while closing sockets", e); } } } }
while(!serverSocket.isClosed()) { try { Socket newClient = serverSocket.accept(); PrintWriter pw = new PrintWriter(newClient.getOutputStream()); if(connections.size() < MAX_CONNECTIONS) { synchronized(this) { pw.print("Too many connections.\r\n"); pw.flush(); newClient.close(); serverSocket.close(); } catch(InterruptedIOException ex) { Thread.currentThread().interrupt();
if (!server.equals("commandline")) { try { ServerSocket serverSocket = new ServerSocket(localPort); serverSocket.close(); } catch (IOException e) { System.err.println("\nPort " + localPort + " already in use!"); throw e; System.err.println(" started in " + (System.currentTimeMillis() - startTime) / 1000. + " s on port " + localPort);
/** * Check whether the port is available to bind * * @param port port * @return -1 means unavailable, otherwise available * @throws IOException */ public static int tryPort(int port) throws IOException { ServerSocket socket = new ServerSocket(port); int rtn = socket.getLocalPort(); socket.close(); return rtn; }
private void storPassif(final String s) throws IOException { final String fileName = removeStartingsSlash(s.substring("STOR ".length())); myOut("150 FILE: " + fileName); final ServerSocket ss = new ServerSocket(port); final Socket incoming = ss.accept(); stor(fileName, incoming); ss.close(); }
public static boolean isAvailablePort(int port) { ServerSocket ss = null; try { ss = new ServerSocket(port); ss.bind(null); return true; } catch (IOException e) { return false; } finally { if (ss != null) { try { ss.close(); } catch (IOException e) { } } } }
/** * Runs in a loop, getting requests from new clients until a client * tells us to exit. */ public void listen() throws IOException { while (stillRunning) { Socket clientSocket = null; try { clientSocket = serverSocket.accept(); log.info("Got a connection"); processRequest(clientSocket); log.info("Goodbye!"); log.info(); } catch (IOException e) { // accidental multiple closes don't seem to have any bad effect clientSocket.close(); log.info(e); continue; } } serverSocket.close(); }
/** * Application method to run the server runs in an infinite loop * listening on port 9898. When a connection is requested, it * spawns a new thread to do the servicing and immediately returns * to listening. The server keeps a unique client number for each * client that connects just to show interesting logging * messages. It is certainly not necessary to do this. */ public static void main(String[] args) throws IOException { System.out.println("The modeling server is running."); int clientNumber = 0; ServerSocket listener = new ServerSocket(9898); try { while (true) { new PerformActionUpdateModel(listener.accept(), clientNumber++).start(); } } finally { listener.close(); } }