How does client data go to sub thread of the server rather than original listening thread?









up vote
0
down vote

favorite












My question is about client server communication in the following scenario.



A server listens on port 21 and is capable of serving multiple incoming connections simultaneously from clients (C1, C2 and so on ) without blocking them



So when a client C1 connects to server S on the port 21 the server accepts the connection on main thread and creates a new thread TH1 and let it handle the client request while the main thread starts listing to port 21 again to accept new clients.



TH1 sends some data back to client C1,
C1 reveives the data and send more data to the server on the same connection which was establisehd on port 21.



This data arrives at server and is received by thread Th1 while the main the main thread of Server continues to listen on port 21.



My question is how does the further data sent by client C1 on the same connection which was made on port 21 automatically goes to the thread Th1 ? even though the main thread of server is listening on port 21 for new connections ?










share|improve this question

























    up vote
    0
    down vote

    favorite












    My question is about client server communication in the following scenario.



    A server listens on port 21 and is capable of serving multiple incoming connections simultaneously from clients (C1, C2 and so on ) without blocking them



    So when a client C1 connects to server S on the port 21 the server accepts the connection on main thread and creates a new thread TH1 and let it handle the client request while the main thread starts listing to port 21 again to accept new clients.



    TH1 sends some data back to client C1,
    C1 reveives the data and send more data to the server on the same connection which was establisehd on port 21.



    This data arrives at server and is received by thread Th1 while the main the main thread of Server continues to listen on port 21.



    My question is how does the further data sent by client C1 on the same connection which was made on port 21 automatically goes to the thread Th1 ? even though the main thread of server is listening on port 21 for new connections ?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My question is about client server communication in the following scenario.



      A server listens on port 21 and is capable of serving multiple incoming connections simultaneously from clients (C1, C2 and so on ) without blocking them



      So when a client C1 connects to server S on the port 21 the server accepts the connection on main thread and creates a new thread TH1 and let it handle the client request while the main thread starts listing to port 21 again to accept new clients.



      TH1 sends some data back to client C1,
      C1 reveives the data and send more data to the server on the same connection which was establisehd on port 21.



      This data arrives at server and is received by thread Th1 while the main the main thread of Server continues to listen on port 21.



      My question is how does the further data sent by client C1 on the same connection which was made on port 21 automatically goes to the thread Th1 ? even though the main thread of server is listening on port 21 for new connections ?










      share|improve this question













      My question is about client server communication in the following scenario.



      A server listens on port 21 and is capable of serving multiple incoming connections simultaneously from clients (C1, C2 and so on ) without blocking them



      So when a client C1 connects to server S on the port 21 the server accepts the connection on main thread and creates a new thread TH1 and let it handle the client request while the main thread starts listing to port 21 again to accept new clients.



      TH1 sends some data back to client C1,
      C1 reveives the data and send more data to the server on the same connection which was establisehd on port 21.



      This data arrives at server and is received by thread Th1 while the main the main thread of Server continues to listen on port 21.



      My question is how does the further data sent by client C1 on the same connection which was made on port 21 automatically goes to the thread Th1 ? even though the main thread of server is listening on port 21 for new connections ?







      multithreading tcp network-programming client-server






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 12:37









      Ahmed

      5,1261568114




      5,1261568114






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You need to provide any package which gets send between client a server with a unique client id. You need a distinguishable hand-shake packet.



          When the server receives a hand-shake packet, it starts a thread, gives it the client id from the packet, and continues listening to port 21.



          Thread then sends whatever is need, say at port 22 to the client and starts listening on a conditional variable.



          When the server's main thread gets a data packet at port 21, it publishes the id and signals the conditional variable. One of the waiting threads will recognize the id and will start working. Alternatively the main thread can created multiple queues and conditional vars, one per thread and wake only a single thread which related to the id. Meanwhile the server continues listening.



          Do not forget to kill the thread either at a good-bye packet or at a timeout.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239023%2fhow-does-client-data-go-to-sub-thread-of-the-server-rather-than-original-listeni%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You need to provide any package which gets send between client a server with a unique client id. You need a distinguishable hand-shake packet.



            When the server receives a hand-shake packet, it starts a thread, gives it the client id from the packet, and continues listening to port 21.



            Thread then sends whatever is need, say at port 22 to the client and starts listening on a conditional variable.



            When the server's main thread gets a data packet at port 21, it publishes the id and signals the conditional variable. One of the waiting threads will recognize the id and will start working. Alternatively the main thread can created multiple queues and conditional vars, one per thread and wake only a single thread which related to the id. Meanwhile the server continues listening.



            Do not forget to kill the thread either at a good-bye packet or at a timeout.






            share|improve this answer
























              up vote
              0
              down vote













              You need to provide any package which gets send between client a server with a unique client id. You need a distinguishable hand-shake packet.



              When the server receives a hand-shake packet, it starts a thread, gives it the client id from the packet, and continues listening to port 21.



              Thread then sends whatever is need, say at port 22 to the client and starts listening on a conditional variable.



              When the server's main thread gets a data packet at port 21, it publishes the id and signals the conditional variable. One of the waiting threads will recognize the id and will start working. Alternatively the main thread can created multiple queues and conditional vars, one per thread and wake only a single thread which related to the id. Meanwhile the server continues listening.



              Do not forget to kill the thread either at a good-bye packet or at a timeout.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                You need to provide any package which gets send between client a server with a unique client id. You need a distinguishable hand-shake packet.



                When the server receives a hand-shake packet, it starts a thread, gives it the client id from the packet, and continues listening to port 21.



                Thread then sends whatever is need, say at port 22 to the client and starts listening on a conditional variable.



                When the server's main thread gets a data packet at port 21, it publishes the id and signals the conditional variable. One of the waiting threads will recognize the id and will start working. Alternatively the main thread can created multiple queues and conditional vars, one per thread and wake only a single thread which related to the id. Meanwhile the server continues listening.



                Do not forget to kill the thread either at a good-bye packet or at a timeout.






                share|improve this answer












                You need to provide any package which gets send between client a server with a unique client id. You need a distinguishable hand-shake packet.



                When the server receives a hand-shake packet, it starts a thread, gives it the client id from the packet, and continues listening to port 21.



                Thread then sends whatever is need, say at port 22 to the client and starts listening on a conditional variable.



                When the server's main thread gets a data packet at port 21, it publishes the id and signals the conditional variable. One of the waiting threads will recognize the id and will start working. Alternatively the main thread can created multiple queues and conditional vars, one per thread and wake only a single thread which related to the id. Meanwhile the server continues listening.



                Do not forget to kill the thread either at a good-bye packet or at a timeout.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 13:52









                Serge

                3,0762912




                3,0762912



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239023%2fhow-does-client-data-go-to-sub-thread-of-the-server-rather-than-original-listeni%23new-answer', 'question_page');

                    );

                    Post as a guest














































































                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    Evgeni Malkin