List of usage examples for java.net ServerSocket bind
public void bind(SocketAddress endpoint) throws IOException
From source file:us.pserver.revok.HttpConnector.java
/** * Create a bounded <code>ServerSocket</code> * connection with this HttpConnector informations. * @return <code>ServerSocket</code>. * @throws IOException In case of creation error. *//*from w w w . j a va2 s . c o m*/ public ServerSocket connectServerSocket() throws IOException { ServerSocket sc = new ServerSocket(); sc.bind(this.createSocketAddress()); return sc; }
From source file:xbird.server.services.RemotePagingService.java
private void acceptConnections(final ServerSocketChannel channel) throws IOException { // set to non blocking mode channel.configureBlocking(false);/* w w w. jav a2 s . c o m*/ // Bind the server socket to the local host and port InetAddress host = InetAddress.getLocalHost(); InetSocketAddress sockAddr = new InetSocketAddress(host, PORT); ServerSocket socket = channel.socket(); //socket.setReuseAddress(true); socket.bind(sockAddr); // Register accepts on the server socket with the selector. This // step tells the selector that the socket wants to be put on the // ready list when accept operations occur. Selector selector = Selector.open(); ByteBuffer cmdBuffer = ByteBuffer.allocateDirect(MAX_COMMAND_BUFLEN); IOHandler ioHandler = new IOHandler(cmdBuffer); AcceptHandler acceptHandler = new AcceptHandler(ioHandler); channel.register(selector, SelectionKey.OP_ACCEPT, acceptHandler); int n; while ((n = selector.select()) > 0) { // Someone is ready for I/O, get the ready keys Set<SelectionKey> selectedKeys = selector.selectedKeys(); final Iterator<SelectionKey> keyItor = selectedKeys.iterator(); while (keyItor.hasNext()) { SelectionKey key = keyItor.next(); keyItor.remove(); // The key indexes into the selector so you // can retrieve the socket that's ready for I/O Handler handler = (Handler) key.attachment(); try { handler.handle(key); } catch (CancelledKeyException cke) { ; } catch (IOException ioe) { LOG.fatal(ioe); NIOUtils.cancelKey(key); } catch (Throwable e) { LOG.fatal(e); NIOUtils.cancelKey(key); } } } }
From source file:org.devtcg.rojocam.rtsp.AbstractRtspServer.java
public void bind(InetSocketAddress addr) throws IOException { ServerSocket socket = new ServerSocket(); socket.bind(addr); mSocket = socket;// ww w . j a va 2 s . c o m Log.i(TAG, "Bound to port " + mSocket.getLocalPort()); }
From source file:org.apache.catalina.cluster.tcp.ReplicationListener.java
public void listen() throws Exception { doListen = true;//from w w w . j a v a 2 s.c o m // allocate an unbound server socket channel ServerSocketChannel serverChannel = ServerSocketChannel.open(); // Get the associated ServerSocket to bind it with ServerSocket serverSocket = serverChannel.socket(); // create a new Selector for use below Selector selector = Selector.open(); // set the port the server channel will listen to serverSocket.bind(new InetSocketAddress(bind, port)); // set non-blocking mode for the listening socket serverChannel.configureBlocking(false); // register the ServerSocketChannel with the Selector serverChannel.register(selector, SelectionKey.OP_ACCEPT); while (doListen) { // this may block for a long time, upon return the // selected set contains keys of the ready channels try { //System.out.println("Selecting with timeout="+timeout); int n = selector.select(timeout); //System.out.println("select returned="+n); if (n == 0) { continue; // nothing to do } // get an iterator over the set of selected keys Iterator it = selector.selectedKeys().iterator(); // look at each key in the selected set while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); // Is a new connection coming in? if (key.isAcceptable()) { ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel channel = server.accept(); registerChannel(selector, channel, SelectionKey.OP_READ, new ObjectReader(channel, selector, callback)); } // is there data to read on this channel? //System.out.println("key readable="+key.isReadable()); if (key.isReadable()) { readDataFromSocket(key); } else { //System.out.println("This shouldn't get called"); key.interestOps(key.interestOps() & (~key.OP_WRITE)); } // remove key from selected set, it's been handled it.remove(); } //System.out.println("Done with loop"); } catch (java.nio.channels.CancelledKeyException nx) { log.warn("Replication client disconnected, error when polling key. Ignoring client."); } catch (Exception x) { log.error("Unable to process request in ReplicationListener", x); } } //while serverChannel.close(); selector.close(); }
From source file:de.uniluebeck.itm.spyglass.gui.wizard.WisebedPacketReaderConfigurationWizard.java
private boolean checkIfServerSocketCanBeOpened(String host, int port) { try {// w w w . j a va2 s.c om ServerSocket socket = new ServerSocket(); socket.bind(new InetSocketAddress(host, port)); socket.close(); return true; } catch (IOException e) { return false; } }
From source file:com.adeptj.runtime.server.Server.java
private boolean isPortAvailable(int port) { boolean portAvailable = false; ServerSocket socket = null; try (ServerSocketChannel socketChannel = ServerSocketChannel.open()) { socket = socketChannel.socket(); socket.setReuseAddress(true);/*from w ww. j av a 2 s. c o m*/ socket.bind(new InetSocketAddress(port)); portAvailable = true; } catch (BindException ex) { LOGGER.error("BindException while acquiring port: [{}], cause:", port, ex); } catch (IOException ex) { LOGGER.error("IOException while acquiring port: [{}], cause:", port, ex); } finally { if (socket != null) { try { socket.close(); } catch (IOException ex) { LOGGER.error("IOException while closing socket!!", ex); } } } return portAvailable; }
From source file:org.apache.axis2.clustering.tribes.WkaBasedMembershipScheme.java
private int getLocalPort(ServerSocket socket, String hostname, int port) throws IOException { InetSocketAddress addr;/*from w w w. jav a 2s .c o m*/ addr = new InetSocketAddress(hostname, port); socket.bind(addr); log.info("Receiver Server Socket bound to:" + addr); socket.setSoTimeout(5); socket.close(); try { Thread.sleep(100); } catch (InterruptedException ignored) { ignored.printStackTrace(); } return port; }
From source file:org.apache.camel.itest.http.HttpTestServer.java
/** * Starts this test server.// w w w. j a va 2s.com */ public void start() throws Exception { if (servicedSocket != null) { throw new IllegalStateException(this.toString() + " already running"); } ServerSocket ssock; if (sslcontext != null) { SSLServerSocketFactory sf = sslcontext.getServerSocketFactory(); ssock = sf.createServerSocket(); } else { ssock = new ServerSocket(); } ssock.setReuseAddress(true); // probably pointless for port '0' ssock.bind(TEST_SERVER_ADDR); servicedSocket = ssock; listenerThread = new ListenerThread(); listenerThread.setDaemon(false); listenerThread.start(); }
From source file:org.apache.http.localserver.LocalTestServer.java
/** * Starts this test server.//from www . ja v a2s. com * Use {@link #getServicePort getServicePort} * to obtain the port number afterwards. */ public void start() throws Exception { if (servicedSocket != null) throw new IllegalStateException(this.toString() + " already running"); ServerSocket ssock; if (sslcontext != null) { SSLServerSocketFactory sf = sslcontext.getServerSocketFactory(); ssock = sf.createServerSocket(); } else { ssock = new ServerSocket(); } ssock.setReuseAddress(true); // probably pointless for port '0' ssock.bind(TEST_SERVER_ADDR); servicedSocket = ssock; listenerThread = new Thread(new RequestListener()); listenerThread.setDaemon(false); listenerThread.start(); }