本文整理汇总了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()));
}
}
}
示例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());
}
示例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();
}
示例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;
}
示例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();
}
}
示例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;
}
}
示例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();
}
}
示例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();
}
示例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;
}
示例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();
}
示例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);
}
}
示例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;
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}