当前位置: 首页>>代码示例>>Java>>正文


Java ServerSocket.close方法代码示例

本文整理汇总了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());
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:17,代码来源:TestServletRestTransport.java

示例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();
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:18,代码来源:TestVertxRestTransport.java

示例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();
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:27,代码来源:TestServer.java

示例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);
}
 
开发者ID:ripreal,项目名称:V8LogScanner,代码行数:24,代码来源:TestScanProfile.java

示例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;
    }
}
 
开发者ID:HY-ZhengWei,项目名称:hy.common.base,代码行数:35,代码来源:Help.java

示例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);
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:WSTest.java

示例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;
    }
}
 
开发者ID:shlee89,项目名称:athena,代码行数:12,代码来源:TestTools.java

示例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();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:29,代码来源:TestApplication.java

示例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();
	}
}
 
开发者ID:thehen101,项目名称:RainServer,代码行数:36,代码来源:ThreadConnectionListener.java

示例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");
}
 
开发者ID:HubSpot,项目名称:NioSmtpClient,代码行数:15,代码来源:IntegrationTest.java

示例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));
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:10,代码来源:TestNetUtils.java

示例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;
	}
}
 
开发者ID:zhangjunfang,项目名称:util,代码行数:12,代码来源:SocketUtils.java

示例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());
}
 
开发者ID:cescoffier,项目名称:introduction-to-vert.x,代码行数:14,代码来源:MyFirstVerticleTest.java

示例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();
        }
    }

}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:43,代码来源:JSSESocketFactory.java

示例15: tryToClose

import java.net.ServerSocket; //导入方法依赖的package包/类
private void tryToClose(@Nullable ServerSocket ss) {
	try {
		if (ss != null) ss.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
开发者ID:rafjordao,项目名称:Nird2,代码行数:8,代码来源:DevReportServer.java


注:本文中的java.net.ServerSocket.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。