Java Code Examples for java.net.ServerSocket#setReuseAddress()
The following examples show how to use
java.net.ServerSocket#setReuseAddress() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TcpSocketServer.java From react-native-tcp-socket with MIT License | 6 votes |
public TcpSocketServer(final ConcurrentHashMap<Integer, TcpSocketClient> socketClients, final TcpReceiverTask.OnDataReceivedListener receiverListener, final Integer id, final ReadableMap options) throws IOException { super(id); // Get data from options int port = options.getInt("port"); String address = options.getString("host"); this.socketClients = socketClients; clientSocketIds = (1 + getId()) * 1000; // Get the addresses InetAddress localInetAddress = InetAddress.getByName(address); // Create the socket serverSocket = new ServerSocket(port, 50, localInetAddress); // setReuseAddress try { boolean reuseAddress = options.getBoolean("reuseAddress"); serverSocket.setReuseAddress(reuseAddress); } catch (Exception e) { // Default to true serverSocket.setReuseAddress(true); } mReceiverListener = receiverListener; listen(); }
Example 2
Source File: GfxdTSSLServerSocket.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Creates a server socket from underlying socket object */ public GfxdTSSLServerSocket(ServerSocket serverSocket, InetSocketAddress bindAddress, SocketParameters params) throws TTransportException { this.socketParams = params; try { this.serverSocket = serverSocket; // Prevent 2MSL delay problem on server restarts serverSocket.setReuseAddress(true); // Bind to listening port if (!serverSocket.isBound()) { // backlog hardcoded to 100 as in TSSLTransportFactory serverSocket.bind(bindAddress, 100); } } catch (IOException ioe) { throw new TTransportException(TTransportException.NOT_OPEN, "Could not bind to host:port " + bindAddress.toString(), ioe); } }
Example 3
Source File: Receiver.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * Starts up the server. The server that has started accepts and handles TCP requests. * * @throws IOException if I/O error occurred at startup */ public void startup() throws IOException { // Create endpoint and inform about starting LOG.info("Starting TCP server bound to " + StringUtils.toString(endpoint)); // Check preconditions verifyStartable(); // Bind sockets for (final SelectionKey selectionKey : selector.keys()) { // Bind server socket try { final ServerSocket serverSocket = ((ServerSocketChannel) selectionKey.channel()).socket(); serverSocket.setReceiveBufferSize(SystemProperty.BUFFER_SIZE); serverSocket.setReuseAddress(true); serverSocket.bind(endpoint); } catch (final BindException e) { throw createDetailedBindException(e, endpoint); } } // Start selector thread selectorThread.start(); }
Example 4
Source File: GfxdTSSLServerSocket.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Creates a server socket from underlying socket object */ public GfxdTSSLServerSocket(ServerSocket serverSocket, InetSocketAddress bindAddress, SocketParameters params) throws TTransportException { this.socketParams = params; try { this.serverSocket = serverSocket; // Prevent 2MSL delay problem on server restarts serverSocket.setReuseAddress(true); // Bind to listening port if (!serverSocket.isBound()) { // backlog hardcoded to 100 as in TSSLTransportFactory serverSocket.bind(bindAddress, 100); } } catch (IOException ioe) { throw new TTransportException(TTransportException.NOT_OPEN, "Could not bind to host:port " + bindAddress.toString(), ioe); } }
Example 5
Source File: GfxdTServerSocket.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Creates a port listening server socket */ public GfxdTServerSocket(InetSocketAddress bindAddress, boolean blocking, boolean clientBlocking, SocketParameters params) throws TTransportException { this.clientBlocking = clientBlocking; this.socketParams = params; try { // Make server socket this.serverSockChannel = ServerSocketChannel.open(); this.serverSockChannel.configureBlocking(blocking); ServerSocket socket = this.serverSockChannel.socket(); // Prevent 2MSL delay problem on server restarts socket.setReuseAddress(true); // Bind to listening port socket.bind(bindAddress); } catch (IOException ioe) { throw new TTransportException(TTransportException.NOT_OPEN, "Could not bind to host:port " + bindAddress.toString(), ioe); } }
Example 6
Source File: AmqpPortImplTest.java From qpid-broker-j with Apache License 2.0 | 5 votes |
private ServerSocket openSocket() throws IOException { ServerSocket serverSocket = new ServerSocket(); serverSocket.setReuseAddress(true); serverSocket.bind(new InetSocketAddress(0)); return serverSocket; }
Example 7
Source File: NetUtils.java From incubator-hivemall with Apache License 2.0 | 5 votes |
public static int getAvailablePort() { try { ServerSocket s = new ServerSocket(0); s.setReuseAddress(true); s.close(); return s.getLocalPort(); } catch (IOException e) { throw new IllegalStateException("Failed to find an available port", e); } }
Example 8
Source File: ConnectionManager.java From rcrs-server with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** Listen for connections on a particular port. @param port The port to listen on. @param registry The registry to install in new connections. @param listener A ConnectionManagerListener that will be informed of new connections. @throws IOException If there is a problem listening on the port. */ public void listen(int port, Registry registry, ConnectionManagerListener listener) throws IOException { synchronized (lock) { if (shutdown) { throw new IOException("Connection manager has been shut down"); } Logger.info("Listening for connections on port " + port); ServerSocket socket = new ServerSocket(port); socket.setSoTimeout(1000); socket.setReuseAddress(true); Reader r = new Reader(socket, registry, listener); readers.add(r); r.start(); } }
Example 9
Source File: EchoServer.java From portforward with Apache License 2.0 | 5 votes |
public EchoServer(InetSocketAddress from) throws IOException { this.from = from; serverSocket = new ServerSocket(); serverSocket.setReuseAddress(true); serverSocket.bind(from); String hostname = from.getHostName(); if (hostname == null) hostname = "*"; log.info("Ready to accept client connection on " + hostname + ":" + from.getPort()); }
Example 10
Source File: NettyBroadcastServiceTest.java From atomix with Apache License 2.0 | 5 votes |
private static int findAvailablePort(int defaultPort) { try { ServerSocket socket = new ServerSocket(0); socket.setReuseAddress(true); int port = socket.getLocalPort(); socket.close(); return port; } catch (IOException ex) { return defaultPort; } }
Example 11
Source File: Monitor.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
public Monitor(int port, String key, Server server) throws IOException { this.server = server; if (port <= 0) { throw new IllegalStateException("Bad stop port"); } if (key == null) { throw new IllegalStateException("Bad stop key"); } this.key = key; setDaemon(true); setName("StopJettyPluginMonitor"); serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); serverSocket.setReuseAddress(true); }
Example 12
Source File: VertxRestServiceTest.java From atomix with Apache License 2.0 | 5 votes |
protected static int findAvailablePort(int defaultPort) { try { ServerSocket socket = new ServerSocket(0); socket.setReuseAddress(true); int port = socket.getLocalPort(); socket.close(); return port; } catch (IOException ex) { return defaultPort; } }
Example 13
Source File: AbstractClientServerSupport.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
protected static ServerSocket openServerSocket(final InetAddress address, final int port, final boolean reuseAddress, final int socketTimeout) throws IOException { final ServerSocket serverSocket = new ServerSocket(); serverSocket.setReuseAddress(reuseAddress); serverSocket.setSoTimeout(socketTimeout); serverSocket.bind(new InetSocketAddress(address, port)); return serverSocket; }
Example 14
Source File: Debugger.java From jpexs-decompiler with GNU General Public License v3.0 | 5 votes |
@Override public void run() { try { ss = new ServerSocket(port, 50, InetAddress.getByName("localhost")); ss.setReuseAddress(true); while (true) { Socket s = ss.accept(); DebugHandler h = new DebugHandler(port, s); handlers.put(h.id, h); h.start(); } } catch (IOException ex) { //ignore } }
Example 15
Source File: LocalAppiumManager.java From AppiumTestDistribution with GNU General Public License v3.0 | 5 votes |
@Override public int getAvailablePort(String hostMachine) throws IOException { ServerSocket socket = new ServerSocket(0); socket.setReuseAddress(true); int port = socket.getLocalPort(); socket.close(); return port; }
Example 16
Source File: S_MVP.java From styT with Apache License 2.0 | 4 votes |
void setupListener() throws IOException { listenSocket = new ServerSocket(); listenSocket.setReuseAddress(true); listenSocket.bind(new InetSocketAddress(port)); }
Example 17
Source File: UsbServer.java From slide-android with GNU General Public License v2.0 | 4 votes |
@Override public void run() { this.running = true; try { server = new ServerSocket(getPort()); server.setReuseAddress(true); if (!server.isBound()) { server.bind(new InetSocketAddress(getPort())); } if (!server.isClosed()) { client = server.accept(); client.setTcpNoDelay(true); output = new ObjectOutputStream(client.getOutputStream()); output.flush(); } } catch (IOException e) { e.printStackTrace(); } //connected if (client != null) { final short sensitivity = AppSettings.getInstance().getSystemSettings().getMouseSensitivity(); if (AppSettings.getInstance().getSettingsElements().getPositioningMode() == PositioningMode.ABSOLUTE) { AppSettings.getInstance().getConnectionManager() .getUsbConnectionManager() .send(PositioningMode.ABSOLUTE, sensitivity); } else { AppSettings.getInstance().getConnectionManager().getUsbConnectionManager().send( PositioningMode.RELATIVE, sensitivity); } SettingsActivity.getActivity().startActivity( AppSettings.getInstance().getActivitySettings() .getCanvasIntent()); // Load the Canvas } }
Example 18
Source File: TurnServer.java From sctalk with Apache License 2.0 | 4 votes |
/** * Function to start the server * * @throws IOException * @throws TurnException */ public void start() throws IOException, TurnException { if (localAddress == null) { throw new RuntimeException("Local address not initialized"); } AllocationRequestListener allocationRequestListner = new AllocationRequestListener(turnStack); ChannelBindRequestListener channelBindRequestListener = new ChannelBindRequestListener(turnStack); ConnectionBindRequestListener connectionBindRequestListener = new ConnectionBindRequestListener(turnStack); ConnectRequestListener connectRequestListener = new ConnectRequestListener(turnStack); CreatePermissionRequestListener createPermissionRequestListener = new CreatePermissionRequestListener(turnStack); RefreshRequestListener refreshRequestListener = new RefreshRequestListener(turnStack); BindingRequestListener bindingRequestListener = new BindingRequestListener(turnStack); SendIndicationListener sendIndListener = new SendIndicationListener(turnStack); sendIndListener.setLocalAddress(localAddress); allocationRequestListner.start(); channelBindRequestListener.start(); connectionBindRequestListener.start(); connectRequestListener.start(); createPermissionRequestListener.start(); refreshRequestListener.start(); bindingRequestListener.start(); sendIndListener.start(); logger.debug( "Local address - " + localAddress.getHostAddress() + ":" + localAddress.getPort()); // instance a server socket for TCP ServerSocket tcpServerSocket = new ServerSocket(localAddress.getPort(), backlog, localAddress.getAddress()); // set reuse to allow binding the socket to the same address tcpServerSocket.setReuseAddress(true); logger.debug("Adding a TCP server socket - " + tcpServerSocket.getLocalSocketAddress()); // create ICE socket wrapper for TCP turnTcpServerSocket = new IceTcpServerSocketWrapper(tcpServerSocket, turnStack.getComponent()); // instance a datagram socket for UDP SafeCloseDatagramSocket udpServerSocket = new SafeCloseDatagramSocket(localAddress.getPort(), localAddress.getAddress()); // set reuse to allow binding the datagram socket to the same address udpServerSocket.setReuseAddress(true); logger.debug("Adding a UDP server socket - " + udpServerSocket.getLocalSocketAddress()); // create ICE socket wrapper for UDP turnUdpSocket = new IceUdpSocketWrapper(udpServerSocket); // add the TCP socket to the stack turnStack.addSocket(turnTcpServerSocket); // add the UDP socket to the stack turnStack.addSocket(turnUdpSocket); started = true; logger.info("Server started, listening on " + localAddress.getAddress() + ":" + localAddress.getPort()); }
Example 19
Source File: S_MVP.java From stynico with MIT License | 4 votes |
void setupListener() throws IOException { listenSocket = new ServerSocket(); listenSocket.setReuseAddress(true); listenSocket.bind(new InetSocketAddress(port)); }
Example 20
Source File: AEProxyImpl.java From BiglyBT with GNU General Public License v2.0 | 4 votes |
public AEProxyImpl( int _port, long _connect_timeout, long _read_timeout, AEProxyHandler _proxy_handler ) throws AEProxyException { port = _port; connect_timeout = _connect_timeout; read_timeout = _read_timeout; proxy_handler = _proxy_handler; String name = "Proxy:" + port; read_selector = new VirtualChannelSelector( name, VirtualChannelSelector.OP_READ, false ); connect_selector = new VirtualChannelSelector( name, VirtualChannelSelector.OP_CONNECT, true ); write_selector = new VirtualChannelSelector( name, VirtualChannelSelector.OP_WRITE, true ); try{ ssc = ServerSocketChannel.open(); ServerSocket ss = ssc.socket(); ss.setReuseAddress(true); ss.bind( new InetSocketAddress( InetAddress.getByName("127.0.0.1"), port), 128 ); if ( port == 0 ){ port = ss.getLocalPort(); } new AEThread2("AEProxy:connect.loop") { @Override public void run() { selectLoop( connect_selector ); } }.start(); new AEThread2("AEProxy:read.loop") { @Override public void run() { selectLoop( read_selector ); } }.start(); new AEThread2("AEProxy:write.loop") { @Override public void run() { selectLoop( write_selector ); } }.start(); new AEThread2("AEProxy:accept.loop") { @Override public void run() { acceptLoop( ssc ); } }.start(); if (Logger.isEnabled()) Logger.log(new LogEvent(LOGID, "AEProxy: listener established on port " + port)); }catch( Throwable e){ Logger.logTextResource(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, "Tracker.alert.listenfail"), new String[] { "" + port + " (proxy)" }); if (Logger.isEnabled()) Logger.log(new LogEvent(LOGID, "AEProxy: listener failed on port " + port, e)); throw( new AEProxyException( "AEProxy: accept fails: " + e.toString())); } }