本文整理汇总了Java中java.net.ServerSocket.close方法的典型用法代码示例。如果您正苦于以下问题:Java ServerSocket.close方法的具体用法?Java ServerSocket.close怎么用?Java ServerSocket.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.ServerSocket
的用法示例。
在下文中一共展示了ServerSocket.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
示例2: 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();
}
示例3: testBindError
import java.net.ServerSocket; //导入方法依赖的package包/类
@Test
public void testBindError() 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();
conf.set("TestRange", min+"-"+min);
ServerSocket socket2 = new ServerSocket();
InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
boolean caught = false;
try {
Server.bind(socket2, address2, 10, conf, "TestRange");
} catch (BindException e) {
caught = true;
} finally {
socket2.close();
}
assertTrue("Failed to catch the expected bind exception",caught);
} finally {
socket.close();
}
}
示例4: testSerialisationGetCfgPaths
import java.net.ServerSocket; //导入方法依赖的package包/类
@Test
public void testSerialisationGetCfgPaths() {
SocketTemplates templates = SocketTemplates.instance();
ServerSocket server = templates.createServerSocket(Constants.serverPort);
assertNotNull(server);
String ip = templates.getHostIP(server);
Socket client = templates.createClientSocket(ip, Constants.serverPort);
V8LanLogScannerClient clientScanner = new V8LanLogScannerClient();
V8LogScannerData dataToClient = new V8LogScannerData(V8LogScannerData.ScannerCommands.GET_CFG_PATHS);
dataToClient.putData("cfgPaths", clientScanner.getCfgPaths());
boolean sent = templates.sendData(templates.getOutDataReader(client), dataToClient);
try {
server.close();
} catch (Exception e) {
}
assertTrue(sent);
}
示例5: isPortOpen
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* 判断本机端口是否有效
*
* @author ZhengWei(HY)
* @createDate 2017-01-12
* @version v1.0
*
* @param i_Port 本机端口号
* @return
*/
public final static boolean isPortOpen(int i_Port)
{
ServerSocket v_Server = Help.getServerSocket(i_Port);
if ( v_Server != null )
{
try
{
v_Server.close();
}
catch (Exception exce)
{
// Nothing.
}
v_Server = null;
return true;
}
else
{
return false;
}
}
示例6: main
import java.net.ServerSocket; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
// find a free port
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
ss.close();
Endpoint endPoint1 = null;
Endpoint endPoint2 = null;
try {
endPoint1 = Endpoint.publish("http://0.0.0.0:" + port + "/method1",
new Method1());
endPoint2 = Endpoint.publish("http://0.0.0.0:" + port + "/method2",
new Method2());
System.out.println("Sleep 3 secs...");
Thread.sleep(3000);
} finally {
stop(endPoint2);
stop(endPoint1);
}
}
示例7: 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;
}
}
示例8: main
import java.net.ServerSocket; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
// Some tests require the application to exit immediately
if (args.length > 0 && args[0].equals("-exit")) {
return;
}
// bind to a random port
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
int pid = -1;
try {
pid = ProcessTools.getProcessId();
} catch (Exception e) {
e.printStackTrace();
}
// signal test that we are started - do not remove these lines!!
System.out.println("port:" + port);
System.out.println("pid:" + pid);
System.out.println("waiting for the manager ...");
System.out.flush();
// wait for manager to connect
Socket s = ss.accept();
s.close();
ss.close();
}
示例9: run
import java.net.ServerSocket; //导入方法依赖的package包/类
@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(this.port, this.backlog, this.bindAddress);
System.out.println("ServerSocket bound to " + ss.getLocalSocketAddress().toString());
while (this.shouldListen) {
System.out.println("Listening for client connection on " + ss.getLocalSocketAddress() + " "
+ this.connectedClients.size());
Socket client = ss.accept();
// if we have a filter, check this IP doesn't have too many connections open
if (this.filter != null) {
this.verifyThreads(this.connectedClients);
String IP = client.getInetAddress().getHostAddress();
String connect = this.filter.shouldAcceptConnection(IP, connectedClients);
if (connect != null) {
client.getOutputStream().write(connect.getBytes());
client.getOutputStream().flush();
client.close();
System.out.println("Dropped " + IP + " as: " + connect);
continue;
}
}
System.out.println("Client connected");
client.setSoTimeout(this.maxIdleTimeMS);
ThreadSocketHandler handler = new ThreadSocketHandler(
client, this.server, this.maxMessageSizeBytes);
this.connectedClients.add(handler);
handler.start();
this.verifyThreads(this.connectedClients);
}
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: getFreePort
import java.net.ServerSocket; //导入方法依赖的package包/类
private synchronized static int getFreePort() {
for (int port = 20000; port <= 30000; port++) {
try {
ServerSocket socket = new ServerSocket(port);
socket.setReuseAddress(true);
socket.close();
return port;
} catch (IOException ignored) {
// ignore
}
}
throw new RuntimeException("Could not find a port to listen on");
}
示例11: testCanTcpListenYes
import java.net.ServerSocket; //导入方法依赖的package包/类
@Test
public void testCanTcpListenYes() throws IOException {
InetAddress address = InetAddress.getByName("127.0.0.1");
ServerSocket ss = new ServerSocket(0, 0, address);
int port = ss.getLocalPort();
ss.close();
Assert.assertTrue(NetUtils.canTcpListen(address, port));
}
示例12: isPortAvailable
import java.net.ServerSocket; //导入方法依赖的package包/类
@Override
protected boolean isPortAvailable(int port) {
try {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port, 1,
InetAddress.getByName("localhost"));
serverSocket.close();
return true;
} catch (Exception ex) {
return false;
}
}
示例13: setUp
import java.net.ServerSocket; //导入方法依赖的package包/类
@Before
public void setUp(TestContext context) throws IOException {
vertx = Vertx.vertx();
// Pick an available and random
ServerSocket socket = new ServerSocket(0);
port = socket.getLocalPort();
socket.close();
DeploymentOptions options = new DeploymentOptions()
.setConfig(new JsonObject().put("HTTP_PORT", port));
vertx.deployVerticle(MyFirstVerticle.class.getName(), options, context.asyncAssertSuccess());
}
示例14: checkConfig
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Checks that the certificate is compatible with the enabled cipher suites.
* If we don't check now, the JIoEndpoint can enter a nasty logging loop.
* See bug 45528.
*/
private void checkConfig() throws IOException {
// Create an unbound server socket
ServerSocket socket = sslProxy.createServerSocket();
initServerSocket(socket);
try {
// Set the timeout to 1ms as all we care about is if it throws an
// SSLException on accept.
socket.setSoTimeout(1);
socket.accept();
// Will never get here - no client can connect to an unbound port
} catch (SSLException ssle) {
// SSL configuration is invalid. Possibly cert doesn't match ciphers
IOException ioe = new IOException(sm.getString(
"jsse.invalid_ssl_conf", ssle.getMessage()));
ioe.initCause(ssle);
throw ioe;
} catch (Exception e) {
/*
* Possible ways of getting here
* socket.accept() throws a SecurityException
* socket.setSoTimeout() throws a SocketException
* socket.accept() throws some other exception (after a JDK change)
* In these cases the test won't work so carry on - essentially
* the behaviour before this patch
* socket.accept() throws a SocketTimeoutException
* In this case all is well so carry on
*/
} finally {
// Should be open here but just in case
if (!socket.isClosed()) {
socket.close();
}
}
}
示例15: tryToClose
import java.net.ServerSocket; //导入方法依赖的package包/类
private void tryToClose(@Nullable ServerSocket ss) {
try {
if (ss != null) ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}