Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ServerSocketChannel接受来自同一客户端的更多连接_Java_Sockets_Serversocket_Server_Socketchannel - Fatal编程技术网

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

Java ServerSocketChannel接受来自同一客户端的更多连接,java,sockets,serversocket,server,socketchannel,Java,Sockets,Serversocket,Server,Socketchannel,我的服务器需要接受来自同一客户端的新连接。因为我的客户在连接10小时后重新登录。它是iscsi协议的服务器/客户端,我的客户端是microsoft iscsi启动器 所以我想要个建议。今天,我有一个大小为4的线程池,因此我的服务器可以接受来自同一客户机的4次重新登录。但这个数字可能会随着我的上网时间的增加而增加。代码如下。。。。我应该终止线程池并再次提交连接吗?这是更好的方法吗 提前谢谢 ExecutorService threadPool = Executors.newFixedThreadP

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

所以我想要个建议。今天,我有一个大小为4的线程池,因此我的服务器可以接受来自同一客户机的4次重新登录。但这个数字可能会随着我的上网时间的增加而增加。代码如下。。。。我应该终止线程池并再次提交连接吗?这是更好的方法吗

提前谢谢

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;
                }
            }