Example usage for javax.net ServerSocketFactory getDefault

List of usage examples for javax.net ServerSocketFactory getDefault

Introduction

In this page you can find the example usage for javax.net ServerSocketFactory getDefault.

Prototype

public static ServerSocketFactory getDefault() 

Source Link

Document

Returns a copy of the environment's default socket factory.

Usage

From source file:org.springframework.integration.ip.tcp.TcpSendingMessageHandlerTests.java

@Test
public void testNioNegotiateSingleNoListen() throws Exception {
    final int port = SocketUtils.findAvailableServerSocket();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    Executors.newSingleThreadExecutor().execute(new Runnable() {
        public void run() {
            int i = 0;
            try {
                ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
                latch.countDown();//  w ww .  j a  va  2  s .c  om
                Socket socket = server.accept();
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Object in = null;
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                if (i == 0) {
                    in = ois.readObject();
                    logger.debug("read object: " + in);
                    oos.writeObject("world!");
                    ois = new ObjectInputStream(socket.getInputStream());
                    oos = new ObjectOutputStream(socket.getOutputStream());
                    in = ois.readObject();
                    logger.debug("read object: " + in);
                    oos.writeObject("world!");
                    ois = new ObjectInputStream(socket.getInputStream());
                    oos = new ObjectOutputStream(socket.getOutputStream());
                }
                in = ois.readObject();
                oos.writeObject("Reply" + (++i));
                socket.close();
                server.close();
            } catch (Exception e) {
                if (i == 0) {
                    e.printStackTrace();
                }
            }
        }
    });
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { new HelloWorldInterceptorFactory(),
            new HelloWorldInterceptorFactory() });
    ccf.setInterceptorFactoryChain(fc);
    ccf.setSingleUse(true);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    done.set(true);
    ccf.stop();
}

From source file:org.springframework.integration.ip.util.SocketUtils.java

public static int findAvailableServerSocket(int seed) {
    for (int i = seed; i < seed + 200; i++) {
        try {/* w ww  .j av  a 2 s.  c  o  m*/
            ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(i);
            sock.close();
            return i;
        } catch (Exception e) {
        }
    }
    throw new RuntimeException("Cannot find a free server socket");
}

From source file:org.wso2.carbon.databridge.receiver.binary.BinaryDataReceiver.java

private void startEventTransmission() throws IOException {
    ServerSocketFactory serversocketfactory = ServerSocketFactory.getDefault();
    ServerSocket serversocket = serversocketfactory
            .createServerSocket(binaryDataReceiverConfiguration.getTCPPort());
    for (int i = 0; i < binaryDataReceiverConfiguration.getSizeOfTCPThreadPool(); i++) {
        tcpReceiverExecutorService.submit(new BinaryTransportReceiver(serversocket));
    }// w ww  .  j  a  v  a2s . c  om
    log.info("Started Binary TCP Transport on port : " + binaryDataReceiverConfiguration.getTCPPort());
}

From source file:org.wso2.carbon.databridge.receiver.binary.internal.BinaryDataReceiver.java

private void startEventTransmission() throws IOException {
    ServerSocketFactory serversocketfactory = ServerSocketFactory.getDefault();
    ServerSocket serversocket = serversocketfactory
            .createServerSocket(binaryDataReceiverConfiguration.getTCPPort());
    Thread thread = new Thread(new BinaryEventServerAcceptor(serversocket));
    thread.start();/*from   w  w w  .  j a v  a  2  s .c  o m*/
    log.info("Started Binary TCP Transport on port : " + binaryDataReceiverConfiguration.getTCPPort());
}