當前位置: 首頁>>代碼示例>>Java>>正文


Java ServerSocket.getLocalPort方法代碼示例

本文整理匯總了Java中java.net.ServerSocket.getLocalPort方法的典型用法代碼示例。如果您正苦於以下問題:Java ServerSocket.getLocalPort方法的具體用法?Java ServerSocket.getLocalPort怎麽用?Java ServerSocket.getLocalPort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.ServerSocket的用法示例。


在下文中一共展示了ServerSocket.getLocalPort方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testServer

import java.net.ServerSocket; //導入方法依賴的package包/類
@Test
public void testServer() throws Exception {
  Config config = Config.getInstance(ConfigMode.Test);
  if (config != null) {
    ConnectionForwarder forwarder = new ConnectionForwarder();
    ELM327 elm = new ELM327Impl();
    elm.setCon(getTestConnection());
    forwarder.setLog(elm.getLog());
    forwarder.createServerSocket(ConnectionForwarder.DEFAULT_PORT);
    forwarder.startServer(elm.getCon());
    ServerSocket serverSocket = forwarder.getServerSocket();
    Socket clientSocket = new Socket("localhost",
        serverSocket.getLocalPort());
    elm.getCon().connect(clientSocket);
    elm.initOBD2();
    if (config.debug) {
      System.out.println(String.format(
          "id: %s\ndescription:%s \ndevice id: %s\nhardware id: %s\nfirmware id:%s\nvoltage:%s\n",
          elm.getId(), elm.getDescription(), elm.getDeviceId(),
          elm.getHardwareId(), elm.getFirmwareId(), elm.getCarVoltage()));
    }
  }
}
 
開發者ID:BITPlan,項目名稱:can4eve,代碼行數:24,代碼來源:TestConnection.java

示例2: testCanInitNotListened

import java.net.ServerSocket; //導入方法依賴的package包/類
@Test
public void testCanInitNotListened() throws IOException {
  ServerSocket ss = new ServerSocket(0);
  int port = ss.getLocalPort();
  ss.close();

  new Expectations(ServletConfig.class) {
    {
      ServletConfig.getLocalServerAddress();
      result = "0.0.0.0:" + port;
    }
  };

  ServletRestTransport transport = new ServletRestTransport();
  Assert.assertFalse(transport.canInit());
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:17,代碼來源:TestServletRestTransport.java

示例3: testCanInitListened

import java.net.ServerSocket; //導入方法依賴的package包/類
@Test
public void testCanInitListened() throws IOException {
  ServerSocket ss = new ServerSocket(0);
  int port = ss.getLocalPort();

  new Expectations(TransportConfig.class) {
    {
      TransportConfig.getAddress();
      result = "0.0.0.0:" + port;
    }
  };

  VertxRestTransport transport = new VertxRestTransport();
  Assert.assertFalse(transport.canInit());

  ss.close();
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:18,代碼來源:TestVertxRestTransport.java

示例4: findLocalPort

import java.net.ServerSocket; //導入方法依賴的package包/類
protected int findLocalPort() {
  int port = -1;
  while (port < 0) {
    try {
      ServerSocket socket = new ServerSocket(0);
      socket.setReuseAddress(true);
      port = socket.getLocalPort();
      try {
        socket.close();
      } catch (IOException e) {
        // Ignore IOException on close()
      }
    } catch (IOException ie) {
      // let it go.
    }
  }
  return port;
}
 
開發者ID:linkedin,項目名稱:cruise-control,代碼行數:19,代碼來源:CruiseControlMetricsReporterTest.java

示例5: testBind

import java.net.ServerSocket; //導入方法依賴的package包/類
@Test
public void testBind() throws Exception {
  Configuration conf = new Configuration();
  ServerSocket socket = new ServerSocket();
  InetSocketAddress address = new InetSocketAddress("0.0.0.0",0);
  socket.bind(address);
  try {
    int min = socket.getLocalPort();
    int max = min + 100;
    conf.set("TestRange", min+"-"+max);
    

    ServerSocket socket2 = new ServerSocket();
    InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
    Server.bind(socket2, address2, 10, conf, "TestRange");
    try {
      assertTrue(socket2.isBound());
      assertTrue(socket2.getLocalPort() > min);
      assertTrue(socket2.getLocalPort() <= max);
    } finally {
      socket2.close();
    }
  } finally {
    socket.close();
  }
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:27,代碼來源:TestServer.java

示例6: connect

import java.net.ServerSocket; //導入方法依賴的package包/類
/**
 * This attempts to open a free port. We'll search for an open port until we find one.
 *
 * @return the port we opened or -1 if we couldn't open one.
 */
private int connect() {
    try {
        serverSocket = new ServerSocket(0);
        return serverSocket.getLocalPort();
    } catch (IOException e) {
        logger.error("Could not listen on port: " + port, e);
        return -1;
    }
}
 
開發者ID:lxxlxx888,項目名稱:Reer,代碼行數:15,代碼來源:Server.java

示例7: getLocalPort

import java.net.ServerSocket; //導入方法依賴的package包/類
/**
 * Port in use.
 */
@Override
public int getLocalPort() {
	ServerSocket s = serverSocket;
	if (s == null) {
		return -1;
	} else {
		return s.getLocalPort();
	}
}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:13,代碼來源:JIoEndpoint.java

示例8: start

import java.net.ServerSocket; //導入方法依賴的package包/類
public void start() throws IOException {
    checkServerNotRunning();

    InetAddress localAddress = InetAddress.getByName(null);
    ServerSocket serverSocket = new ServerSocket(0, 1, localAddress);
    InetSocketAddress localSocketAddress = (InetSocketAddress) serverSocket.getLocalSocketAddress();
    host = localSocketAddress.getHostString();
    port = serverSocket.getLocalPort();

    mockServerThread = new MockServerThread(serverSocket, interactions, waitForConnectionClosed,
            waitForAllExpectedCommands, logger, keyStoreProvider);
    mockServerThread.start();
}
 
開發者ID:philipwhiuk,項目名稱:q-mail,代碼行數:14,代碼來源:MockPop3Server.java

示例9: findFreePort

import java.net.ServerSocket; //導入方法依賴的package包/類
private int findFreePort() {
    int port = 8050;
    try {
        ServerSocket servSock = new ServerSocket(0);
        port = servSock.getLocalPort();
        servSock.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return port;
}
 
開發者ID:datathings,項目名稱:greycat,代碼行數:12,代碼來源:ListenerTest.java

示例10: doServerSide

import java.net.ServerSocket; //導入方法依賴的package包/類
void doServerSide() throws Exception {
    ServerSocket serverSock = new ServerSocket(serverPort);

    // signal client, it's ready to accecpt connection
    serverPort = serverSock.getLocalPort();
    serverReady = true;

    // accept a connection
    Socket socket = serverSock.accept();
    System.out.println("Server: Connection accepted");

    InputStream is = socket.getInputStream();
    OutputStream os = socket.getOutputStream();

    // read the bindRequest
    while (is.read() != -1) {
        // ignore
        is.skip(is.available());
        break;
    }

    byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
                           0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
    // write bindResponse
    os.write(bindResponse);
    os.flush();

    // ignore any more request.
    while (is.read() != -1) {
        // ignore
        is.skip(is.available());
    }

    is.close();
    os.close();
    socket.close();
    serverSock.close();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:39,代碼來源:InvalidLdapFilters.java

示例11: Server4MessagingSystem

import java.net.ServerSocket; //導入方法依賴的package包/類
public Server4MessagingSystem(int port, MessagingSystem ms) {	
	try {
		
		this.logger = ms;
		serverSocket = new ServerSocket(port);
		this.port = serverSocket.getLocalPort();
		
	} catch (IOException e) {
		logger.error(e.getMessage(), Server4MessagingSystem.class.getName(),e);
	}
}
 
開發者ID:eclipse,項目名稱:gemoc-studio,代碼行數:12,代碼來源:Server4MessagingSystem.java

示例12: findAvailablePort

import java.net.ServerSocket; //導入方法依賴的package包/類
public 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;
    }
}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:12,代碼來源:TestTools.java

示例13: getFreeSocketPort

import java.net.ServerSocket; //導入方法依賴的package包/類
public static int getFreeSocketPort() {
    try {
        ServerSocket ss = new ServerSocket(0);
        int freePort = ss.getLocalPort();
        ss.close();
        return freePort;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
開發者ID:liuht777,項目名稱:uncode-scheduler,代碼行數:11,代碼來源:ScheduleUtil.java

示例14: selectPort

import java.net.ServerSocket; //導入方法依賴的package包/類
@BeforeClass
public static void selectPort() {
	try {
		ServerSocket s = new ServerSocket(0);
		int port = s.getLocalPort();
		s.close();
		System.setProperty("jersey.config.test.container.port", Integer.toString(port));
	} catch (IOException e) {
		throw new IllegalStateException(e);
	}
}
 
開發者ID:crnk-project,項目名稱:crnk-framework,代碼行數:12,代碼來源:JerseyTestBase.java

示例15: getFreePort

import java.net.ServerSocket; //導入方法依賴的package包/類
/**
 * Gets a free port. Note: although the returned port is free immediately after this method
 * completes there is no guarantee that it will remain free so callers are advised to use it as
 * soon as posisble after getting it.
 *
 * @return a free port
 */
public static int getFreePort() {
  try {
    ServerSocket socket = new ServerSocket(0);
    int port = socket.getLocalPort();
    socket.close();
    return port;
  } catch (IOException ex) {
    throw new RuntimeException("Failed to get free port!", ex);
  }
}
 
開發者ID:glytching,項目名稱:dragoman,代碼行數:18,代碼來源:NetworkUtils.java


注:本文中的java.net.ServerSocket.getLocalPort方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。