List of usage examples for java.net ServerSocket ServerSocket
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
From source file:com.lithium.flow.vault.AgentServer.java
public AgentServer(@Nonnull Config config) throws IOException { checkNotNull(config);//from w ww .ja va2 s.c om ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(System.in, baos); byte[] bytes = baos.toByteArray(); ServerSocket server = new ServerSocket(config.getInt("agent.port"), -1, InetAddress.getByName(null)); long endTime = System.currentTimeMillis() + config.getTime("agent.maximumTime", "1d"); long inactiveTime = config.getTime("agent.inactiveTime", "8h"); AtomicLong lastTime = new AtomicLong(System.currentTimeMillis()); new LoopThread(() -> { try { Socket socket = server.accept(); log.info("accepted connection: {}", socket); try (OutputStream out = socket.getOutputStream()) { IOUtils.copy(new ByteArrayInputStream(bytes), out); } } catch (IOException e) { // } lastTime.set(System.currentTimeMillis()); }); new LoopThread(1000, () -> { long time = System.currentTimeMillis(); if (time > endTime) { log.info("maximum time reached"); System.exit(0); } if (time > lastTime.get() + inactiveTime) { log.info("inactive time reached"); System.exit(0); } }); log.info("started agent on port {}", server.getLocalPort()); Sleep.forever(); }
From source file:org.apache.flink.runtime.taskmanager.TestManagerStartupTest.java
/** * Tests that the TaskManager fails synchronously when the actor system port * is in use.// w w w . j a v a2 s.c o m */ @Test public void testStartupWhenTaskmanagerActorPortIsUsed() { ServerSocket blocker = null; try { final String localHostName = "localhost"; final InetAddress localAddress = InetAddress.getByName(localHostName); // block some port blocker = new ServerSocket(0, 50, localAddress); final int port = blocker.getLocalPort(); try { TaskManager.runTaskManager(localHostName, port, new Configuration(), StreamingMode.BATCH_ONLY, TaskManager.class); fail("This should fail with an IOException"); } catch (IOException e) { // expected. validate the error message assertNotNull(e.getMessage()); assertTrue(e.getMessage().contains("Address already in use")); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } finally { if (blocker != null) { try { blocker.close(); } catch (IOException e) { // no need to log here } } } }
From source file:org.apache.axis2.transport.tcp.TCPServer.java
public void startServer() throws IOException { if (serverSocket == null) { if (endpoint.getHost() != null) { InetAddress address = InetAddress.getByName(endpoint.getHost()); serverSocket = new ServerSocket(endpoint.getPort(), endpoint.getBacklog(), address); } else {// w w w . j a v a 2 s . c o m serverSocket = new ServerSocket(endpoint.getPort(), endpoint.getBacklog()); } } started = true; endpoint.getListener().getConfigurationContext().getThreadPool().execute(this); log.info("TCP server started on port : " + endpoint.getPort()); }
From source file:com.predic8.membrane.core.transport.http.HttpEndpointListener.java
public HttpEndpointListener(String ip, int port, HttpTransport transport, SSLProvider sslProvider) throws IOException { this.transport = transport; this.sslProvider = sslProvider; try {/*from w w w .j a v a2 s.co m*/ if (sslProvider != null) serverSocket = sslProvider.createServerSocket(port, 50, ip != null ? InetAddress.getByName(ip) : null); else serverSocket = new ServerSocket(port, 50, ip != null ? InetAddress.getByName(ip) : null); setName("Connection Acceptor " + (ip != null ? ip + ":" : ":") + port); log.debug("listening at port " + port + (ip != null ? " ip " + ip : "")); } catch (BindException e) { throw new PortOccupiedException(port); } }
From source file:com.sshtools.j2ssh.agent.SshAgentForwardingListener.java
SshAgentForwardingListener(String sessionId, ConnectionProtocol connection) { log.info("Forwarding agent started"); this.sessionId = sessionId; this.connection = connection; port = selectPort();// ww w. j ava 2 s. com location = "localhost:" + String.valueOf(port); thread = new Thread(new Runnable() { public void run() { state.setValue(StartStopState.STARTED); try { server = new ServerSocket(port, 5, InetAddress.getByName("localhost")); //server.bind(new InetSocketAddress("localhost", port)); Socket socket; while ((state.getValue() == StartStopState.STARTED) && ((socket = server.accept()) != null)) { AgentSocketChannel channel = new AgentSocketChannel(true); channel.bindSocket(socket); if (!SshAgentForwardingListener.this.connection.openChannel(channel)) { log.warn("Failed to open agent forwarding channel"); } } } catch (Exception e) { if (state.getValue() == StartStopState.STARTED) { log.warn("Forwarding agent socket failed", e); } } state.setValue(StartStopState.STOPPED); } }); }
From source file:com.mobius.software.mqtt.performance.controller.net.ClientBootstrap.java
public static boolean available(String host, int port) { try (ServerSocket ss = new ServerSocket(port, 50, InetAddress.getByName(host))) { ss.setReuseAddress(true);//from w ww . java 2 s. c om return true; } catch (IOException e) { } return false; }
From source file:org.npr.android.test.HttpServer.java
/** * Prepare the server to start.// ww w . ja va 2s.c o m * * This only needs to be called once per instance. Once initialized, the * server can be started and stopped as needed. */ public void init() { try { socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })); socket.setSoTimeout(5000); port = socket.getLocalPort(); Log.d(TAG, "Server stated at " + socket.getInetAddress().getHostAddress() + ":" + port); } catch (UnknownHostException e) { Log.e(TAG, "Error initializing server", e); } catch (IOException e) { Log.e(TAG, "Error initializing server", e); } }
From source file:org.apache.flink.runtime.taskmanager.TaskManagerStartupTest.java
/** * Tests that the TaskManager fails synchronously when the actor system port * is in use.// w w w . ja v a 2s. c o m * * @throws Throwable */ @Test(expected = BindException.class) public void testStartupWhenTaskmanagerActorPortIsUsed() throws BindException { ServerSocket blocker = null; try { final String localHostName = "localhost"; final InetAddress localBindAddress = InetAddress.getByName(NetUtils.getWildcardIPAddress()); // block some port blocker = new ServerSocket(0, 50, localBindAddress); final int port = blocker.getLocalPort(); TaskManager.runTaskManager(localHostName, ResourceID.generate(), port, new Configuration(), TaskManager.class); fail("This should fail with an IOException"); } catch (IOException e) { // expected. validate the error message List<Throwable> causes = StartupUtils.getExceptionCauses(e, new ArrayList<Throwable>()); for (Throwable cause : causes) { if (cause instanceof BindException) { throw (BindException) cause; } } fail("This should fail with an exception caused by BindException"); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } finally { if (blocker != null) { try { blocker.close(); } catch (IOException e) { // no need to log here } } } }
From source file:com.sshtools.j2ssh.agent.SshAgentSocketListener.java
/** * Creates a new SshAgentSocketListener object. * * @param location the location of the listening agent. This should be a * random port on the localhost such as localhost:15342 * @param keystore the keystore for agent operation * * @throws AgentNotAvailableException if the location specifies an invalid * location/*from w w w .j a v a2 s . co m*/ */ public SshAgentSocketListener(String location, KeyStore keystore) throws AgentNotAvailableException { log.info("New SshAgent instance created"); // Verify the agent location this.location = location; if (location == null) { throw new AgentNotAvailableException(); } this.location = location; int idx = location.indexOf(":"); if (idx == -1) { throw new AgentNotAvailableException(); } String host = location.substring(0, idx); port = Integer.parseInt(location.substring(idx + 1)); this.keystore = keystore; try { server = new ServerSocket(port, 5, InetAddress.getByName(host)); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.dakror.virtualhub.server.Server.java
public Server() { currentServer = this; dir = new File(CFG.DIR, "Server"); dir.mkdir();/*from w w w. jav a 2 s. c o m*/ frame = new ServerFrame(); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { shutdown(); } }); setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); frame.log("ERROR: " + sw.toString()); } }); try { socket = new ServerSocket(CFG.SERVER_PORT, 0, InetAddress.getLocalHost()); frame.log("Starte Server unter " + socket.getInetAddress().getHostAddress() + ":" + socket.getLocalPort()); } catch (BindException e) { frame.log("Es luft bereits ein Server auf diesem Port!"); shutdown(); return; } catch (Exception e) { e.printStackTrace(); } DBManager.init(); frame.log("Datenbank initialisiert"); start(); }