List of usage examples for java.net InetSocketAddress InetSocketAddress
public InetSocketAddress(int port)
From source file:com.mac.halendpoint.endpoints.HalSocket.java
public HalSocket() { super(new InetSocketAddress(PORT)); ctx = new AnnotationConfigApplicationContext(DataSourceConfig.class); presetRepo = ctx.getBean(PresetRepository.class); isFirstConnection = true;/*from w w w . j a va2s .c om*/ socketManager = new WebSocketManagerImpl(); router = new ProtocolRouter(); deviceManager = new DeviceStateRectifier(); deviceManager.setUpdatedStateListener(this); }
From source file:me.xingrz.prox.tcp.TcpProxy.java
@Override protected ServerSocketChannel createChannel(Selector selector) throws IOException { ServerSocketChannel channel = ServerSocketChannel.open(); channel.configureBlocking(false);/*ww w .jav a 2 s .c o m*/ channel.socket().bind(new InetSocketAddress(0)); channel.register(selector, SelectionKey.OP_ACCEPT, this); return channel; }
From source file:com.l2jfree.network.mmocore.AcceptorThread.java
public void openServerSocket(InetAddress address, int port) throws IOException { ServerSocketChannel selectable = ServerSocketChannel.open(); selectable.configureBlocking(false); ServerSocket ss = selectable.socket(); ss.setReuseAddress(true);/* w ww . j a v a 2 s. com*/ ss.setReceiveBufferSize(getBufferSize()); if (address == null) { ss.bind(new InetSocketAddress(port)); } else { ss.bind(new InetSocketAddress(address, port)); } selectable.register(getSelector(), SelectionKey.OP_ACCEPT); }
From source file:com.rapleaf.hank.ZkTestCase.java
public static void setupZkServer() throws Exception { if (server == null) { LOG.debug("deleting zk data dir (" + zkDir + ")"); File zkDirFile = new File(zkDir); FileUtils.deleteDirectory(zkDirFile); zkDirFile.mkdirs();//from w w w. j av a2s .com server = new ZooKeeperServer(zkDirFile, zkDirFile, TICK_TIME); int clientPort = 2000; while (true) { LOG.debug("Trying to bind server to port " + clientPort); try { standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort)); } catch (BindException e) { LOG.trace("Failed binding ZK Server to client port: " + clientPort); //this port is already in use. try to use another clientPort++; continue; } LOG.debug("Succeeded in binding ZK Server to client port " + clientPort); break; } standaloneServerFactory.startup(server); if (!waitForServerUp(clientPort, CONNECTION_TIMEOUT)) { throw new IOException("Waiting for startup of standalone server"); } zkClientPort = clientPort; } }
From source file:com.hellblazer.utils.deserializers.InetSocketAddressDeserializer.java
@Override protected InetSocketAddress _deserialize(String value, DeserializationContext ctxt) throws IOException, JsonProcessingException { int i = value.indexOf(':'); if (i < 0) { throw new InvalidFormatException(String.format("Must include port: %s", value), value, InetSocketAddress.class); }/*from w w w . j a v a 2 s . c o m*/ String host = value.substring(0, i); int port = Integer.parseInt(value.substring(i + 1)); if (host.isEmpty() || host.equals("*")) { return new InetSocketAddress(port); } return new InetSocketAddress(host, port); }
From source file:com.taobao.ad.easyschedule.server.JobHttpServer.java
@Override public void run() { try {//from w ww . j a v a 2 s .com HttpServer httpServer = HttpServer.create(new InetSocketAddress(Integer.parseInt(Const.SERVER_PORT)), 0); httpServer.createContext(Const.SERVER_CONTEXT, new HttpHandler() { @Override public void handle(final HttpExchange exchange) throws IOException { JobResult result = JobResult.succcessResult(); String queryPath = exchange.getRequestURI().getPath(); String queryStr = exchange.getRequestURI().getQuery(); if (StringUtils.isEmpty(queryStr) || StringUtils.isEmpty(queryPath)) { result = JobResult.errorResult(JobResult.RESULTCODE_PARAMETER_ILLEGAL, "??"); } else { try { queryPath = queryPath.replaceAll(Const.SERVER_CONTEXT, ""); JobData jobData = HttpJobUtils.createJobData(queryStr); String beanId = jobData.getData().get(JobData.JOBDATA_DATA_BEANID); if (StringUtils.isEmpty(beanId)) { result = JobResult.errorResult(JobResult.RESULTCODE_PARAMETER_ILLEGAL, "???"); } else { if (Const.DEPLOY_MODE.equals("dev")) { Long signTime = System.currentTimeMillis() / 1000; String token = TokenUtils.generateToken(signTime.toString()); jobData.setSignTime(signTime.toString()); jobData.setToken(token); } result = HttpJobUtils.checkJobData(jobData); if (result.isSuccess()) { if (queryPath.equals(Const.QUERYPATH_DOSTDJOB)) { WebApplicationContext context = ContextLoader .getCurrentWebApplicationContext(); JobExecutor executor = (JobExecutor) context.getBean(beanId); result = HttpJobExecutor.getInstance().execute(jobData, executor); } else { result = JobResult.errorResult(JobResult.RESULTCODE_PARAMETER_ILLEGAL, "" + queryPath); } } } } catch (Exception e) { result = JobResult.errorResult(JobResult.RESULTCODE_OTHER_ERR, ""); logger.info("|" + queryStr); } } JSONObject json = (JSONObject) JSONObject.toJSON(result); exchange.getResponseHeaders().set("Content-type", "application/json;charset=utf-8"); exchange.sendResponseHeaders(200, 0L); OutputStream os = exchange.getResponseBody(); os.write(json.toString().getBytes("utf-8")); os.close(); } }); httpServer.start(); } catch (Throwable e) { logger.error("JobHttpServer.run is fail:" + e.getMessage(), e); } }
From source file:org.fao.geonet.utils.GeonetHttpRequestFactoryTest.java
@Test public void testReadUrl() throws Exception { final int port = 29483; InetSocketAddress address = new InetSocketAddress(port); HttpServer httpServer = HttpServer.create(address, 0); final Element expectedResponse = new Element("resource").addContent(new Element("id").setText("test")); HttpHandler requestHandler = new HttpHandler() { @Override//from ww w .java2 s .c o m public void handle(HttpExchange exchange) throws IOException { byte[] response = Xml.getString(expectedResponse).getBytes(); exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length); exchange.getResponseBody().write(response); exchange.close(); } }; final String urlPath = "/1234.xml"; httpServer.createContext(urlPath, requestHandler); try { httpServer.start(); final XmlRequest xmlRequest = new GeonetHttpRequestFactory() .createXmlRequest(new URL("http://localhost:" + port + urlPath)); final Element response = xmlRequest.execute(); assertEquals(Xml.getString(expectedResponse), Xml.getString(response)); } finally { httpServer.stop(0); } }
From source file:inet.encode.Monitor.java
public static void startMonitorServer(String... args) { processArgs(args);/*from w ww . jav a 2 s .co m*/ try { server = HttpServer.create(new InetSocketAddress(MONITOR_SERVER_PORT), 0); } catch (IOException ex) { Logger.log(ex); } createContext(); //server.setExecutor(null); server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool()); server.start(); Logger.log("Server monitor started..."); }
From source file:org.apache.servicemix.http.HttpWsdlTest.java
/** * determines a free port number/* w w w. j a va 2s . c o m*/ * @return a free port number */ private static int getFreePortNumber() { int portNumber = usedPortNumber + 1; boolean inUse = true; while (inUse) { try { ServerSocket serverSocket = new ServerSocket(); SocketAddress endpoint = new InetSocketAddress(portNumber); serverSocket.bind(endpoint); serverSocket.close(); inUse = false; usedPortNumber = portNumber; } catch (IOException e) { inUse = true; } } return portNumber; }
From source file:io.selendroid.server.SelendroidStandaloneServer.java
/** * for testing only//from w w w.j a v a2 s . co m * * @throws AndroidSdkException */ protected SelendroidStandaloneServer(SelendroidConfiguration configuration, SelendroidStandaloneDriver driver) throws AndroidSdkException { this.configuration = configuration; this.driver = driver; NamingThreadFactory namingThreadFactory = new NamingThreadFactory(Executors.defaultThreadFactory(), "selendroid-standalone-handler"); webServer = WebServers.createWebServer(Executors.newCachedThreadPool(namingThreadFactory), new InetSocketAddress(configuration.getPort()), URI.create("http://127.0.0.1" + (configuration.getPort() == 80 ? "" : (":" + configuration.getPort())) + "/")); init(); }