ServerSocketChannel接受来自同一客户端的更多连接

时间:2014-11-25 14:16:16

标签: java sockets serversocket server socketchannel

我的服务器需要接受来自同一客户端的新连接。因为我的客户连接10个小时后才重新登录。它是iscsi协议的服务器/客户端,我的客户端是微软iscsi启动器。

所以我想要消化。今天我有一个大小为4的线程池,所以我的服务器可以接受来自同一客户端的4个重新连接。但是这个数字可能会增加,因为我的时间连接很大。代码如下....我应该终止threadPool并再次提交连接吗?这是更好的方法吗?

提前感谢。

ExecutorService threadPool = Executors.newFixedThreadPool(4);
        // Create a blocking server socket and check for connections
        try {
            // Create a blocking server socket channel on the specified/default port
            System.out.println("0");
            serverSocketChannel = ServerSocketChannel.open();

            // Making sure the socket is bound to the address used in the config.
            serverSocketChannel.socket().bind(
                    new InetSocketAddress(getConfig().getTargetAddress(), getConfig().getPort()));
            System.out.println("0.1");

            serverSocketChannel.configureBlocking(true);
            System.out.println("0.2");

            System.out.println("1");

            while (running) {
                System.out.println("2");

                // Accept the connection request.
                // If serverSocketChannel is blocking, this method blocks.
                // The returned channel is in blocking mode.
                final SocketChannel socketChannel = serverSocketChannel.accept();
                System.out.println("3");

                // deactivate Nagle algorithm
                socketChannel.socket().setTcpNoDelay(true);

                connection = new TargetConnection(socketChannel, true);
                System.out.println("4 - ");
                try {
                    final ProtocolDataUnit pdu = connection.receivePdu();
                    // confirm OpCode
                    if (pdu.getBasicHeaderSegment().getOpCode() != OperationCode.LOGIN_REQUEST)
                        throw new InternetSCSIException();
                    // get initiatorSessionID
                    LoginRequestParser parser = (LoginRequestParser) pdu.getBasicHeaderSegment().getParser();
                    ISID initiatorSessionID = parser.getInitiatorSessionID();

                    /*
                     * TODO get (new or existing) session based on TSIH But since we don't do session reinstatement and
                     * MaxConnections=1, we can just create a new one.
                     */
                    TargetSession session = new TargetSession(this, connection, initiatorSessionID,
                            parser.getCommandSequenceNumber(), parser.getExpectedStatusSequenceNumber());
                    // set ExpCmdSN (PDU is immediate, hence no ++)

                    sessions.add(session);
                    System.out.println("5 - sessions.size(): " + sessions.size());
                    threadPool.submit(connection);// ignore returned Future
                    // connection.call();
                } catch (DigestException | InternetSCSIException | SettingsException e) {
                    System.out.println("TargetServer: "
                            + new ToStringBuilder(sessions).reflectionToString(sessions).toString());
                    LOGGER.info("Throws Exception", e);
                    continue;
                }
            }

0 个答案:

没有答案