ServerSocketChannel to accept more connections from the same client

518 views Asked by At

my server needs to accept new connections from the same client. Because my client do relogin after 10 hours connected. It is a server/client for iscsi protocol and my client is a microsoft iscsi initiator.

So I would like a sugestion. Today I have a thread pool with size 4, so my server can accept 4 relogins from the same client. But this number could increment as my time connected is big. The code is below.... Should I terminate the threadPool and submit the connection again? It is the better approach?

thanks in advance.

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

There are 0 answers