当前位置: 首页>>代码示例>>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;未经允许,请勿转载。