How port is reused by multiple node application process












0














When I run the same application which uses os port multiple times in java application(example ) I get below exception.



    Exception in thread "main" java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)


node_cluster.js



    const cluster = require('cluster'),
cpus = require('os').cpus().length;

cluster.setupMaster({
exec: '3_worker.js'
});

if(cluster.isMaster) {
for(let i = 0; i< cpus; i++) {
cluster.fork();
}
cluster.on('fork', worker => console.log(worker.id + " is forked"));
cluster.on('listening', (worker, address) => console.log(worker.id + "is listening on " + JSON.stringify(address)));
cluster.on('online', worker => console.log(`${worker.id} is online`));
cluster.on('disconnect', worker => console.log(`${worker.id} is disconnected`));
cluster.on('exit', (worker, code, signal) => console.log(`${worker.id} is dead due to ${code} and ${signal}`));
}


3_worker.js



    const http = require('http');

http.createServer((req, res) => {
console.log(req.url);
res.writeHead(200);
res.end('hello');
}).listen(8080);


The above programs return result -



    3is listening on {"addressType":4,"address":null,"port":8080}
5is listening on {"addressType":4,"address":null,"port":8080}
4is listening on {"addressType":4,"address":null,"port":8080}
7is listening on {"addressType":4,"address":null,"port":8080}
8is listening on {"addressType":4,"address":null,"port":8080}
2is listening on {"addressType":4,"address":null,"port":8080}
1is listening on {"addressType":4,"address":null,"port":8080}
6is listening on {"addressType":4,"address":null,"port":8080}


The output of ps is -



    $ ps
PID TTY TIME CMD
4477 ttys001 0:00.16 /bin/bash -l
5219 ttys001 0:00.12 node 3_clustering.js
5220 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5221 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5222 ttys001 0:00.13 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5223 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5224 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5225 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5226 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5227 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
5794 ttys002 0:00.05 /bin/bash --rcfile /Applications/IntelliJ IDEA.app/Con
6063 ttys003 0:00.03 /Applications/Utilities/iTerm.app/Contents/MacOS/iTerm
6065 ttys003 0:00.04 -bash


How multiple node.js process are able to listen on same port.










share|improve this question



























    0














    When I run the same application which uses os port multiple times in java application(example ) I get below exception.



        Exception in thread "main" java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)


    node_cluster.js



        const cluster = require('cluster'),
    cpus = require('os').cpus().length;

    cluster.setupMaster({
    exec: '3_worker.js'
    });

    if(cluster.isMaster) {
    for(let i = 0; i< cpus; i++) {
    cluster.fork();
    }
    cluster.on('fork', worker => console.log(worker.id + " is forked"));
    cluster.on('listening', (worker, address) => console.log(worker.id + "is listening on " + JSON.stringify(address)));
    cluster.on('online', worker => console.log(`${worker.id} is online`));
    cluster.on('disconnect', worker => console.log(`${worker.id} is disconnected`));
    cluster.on('exit', (worker, code, signal) => console.log(`${worker.id} is dead due to ${code} and ${signal}`));
    }


    3_worker.js



        const http = require('http');

    http.createServer((req, res) => {
    console.log(req.url);
    res.writeHead(200);
    res.end('hello');
    }).listen(8080);


    The above programs return result -



        3is listening on {"addressType":4,"address":null,"port":8080}
    5is listening on {"addressType":4,"address":null,"port":8080}
    4is listening on {"addressType":4,"address":null,"port":8080}
    7is listening on {"addressType":4,"address":null,"port":8080}
    8is listening on {"addressType":4,"address":null,"port":8080}
    2is listening on {"addressType":4,"address":null,"port":8080}
    1is listening on {"addressType":4,"address":null,"port":8080}
    6is listening on {"addressType":4,"address":null,"port":8080}


    The output of ps is -



        $ ps
    PID TTY TIME CMD
    4477 ttys001 0:00.16 /bin/bash -l
    5219 ttys001 0:00.12 node 3_clustering.js
    5220 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5221 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5222 ttys001 0:00.13 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5223 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5224 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5225 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5226 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5227 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
    5794 ttys002 0:00.05 /bin/bash --rcfile /Applications/IntelliJ IDEA.app/Con
    6063 ttys003 0:00.03 /Applications/Utilities/iTerm.app/Contents/MacOS/iTerm
    6065 ttys003 0:00.04 -bash


    How multiple node.js process are able to listen on same port.










    share|improve this question

























      0












      0








      0







      When I run the same application which uses os port multiple times in java application(example ) I get below exception.



          Exception in thread "main" java.net.BindException: Address already in use
      at sun.nio.ch.Net.bind0(Native Method)
      at sun.nio.ch.Net.bind(Net.java:433)
      at sun.nio.ch.Net.bind(Net.java:425)
      at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)


      node_cluster.js



          const cluster = require('cluster'),
      cpus = require('os').cpus().length;

      cluster.setupMaster({
      exec: '3_worker.js'
      });

      if(cluster.isMaster) {
      for(let i = 0; i< cpus; i++) {
      cluster.fork();
      }
      cluster.on('fork', worker => console.log(worker.id + " is forked"));
      cluster.on('listening', (worker, address) => console.log(worker.id + "is listening on " + JSON.stringify(address)));
      cluster.on('online', worker => console.log(`${worker.id} is online`));
      cluster.on('disconnect', worker => console.log(`${worker.id} is disconnected`));
      cluster.on('exit', (worker, code, signal) => console.log(`${worker.id} is dead due to ${code} and ${signal}`));
      }


      3_worker.js



          const http = require('http');

      http.createServer((req, res) => {
      console.log(req.url);
      res.writeHead(200);
      res.end('hello');
      }).listen(8080);


      The above programs return result -



          3is listening on {"addressType":4,"address":null,"port":8080}
      5is listening on {"addressType":4,"address":null,"port":8080}
      4is listening on {"addressType":4,"address":null,"port":8080}
      7is listening on {"addressType":4,"address":null,"port":8080}
      8is listening on {"addressType":4,"address":null,"port":8080}
      2is listening on {"addressType":4,"address":null,"port":8080}
      1is listening on {"addressType":4,"address":null,"port":8080}
      6is listening on {"addressType":4,"address":null,"port":8080}


      The output of ps is -



          $ ps
      PID TTY TIME CMD
      4477 ttys001 0:00.16 /bin/bash -l
      5219 ttys001 0:00.12 node 3_clustering.js
      5220 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5221 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5222 ttys001 0:00.13 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5223 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5224 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5225 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5226 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5227 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5794 ttys002 0:00.05 /bin/bash --rcfile /Applications/IntelliJ IDEA.app/Con
      6063 ttys003 0:00.03 /Applications/Utilities/iTerm.app/Contents/MacOS/iTerm
      6065 ttys003 0:00.04 -bash


      How multiple node.js process are able to listen on same port.










      share|improve this question













      When I run the same application which uses os port multiple times in java application(example ) I get below exception.



          Exception in thread "main" java.net.BindException: Address already in use
      at sun.nio.ch.Net.bind0(Native Method)
      at sun.nio.ch.Net.bind(Net.java:433)
      at sun.nio.ch.Net.bind(Net.java:425)
      at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)


      node_cluster.js



          const cluster = require('cluster'),
      cpus = require('os').cpus().length;

      cluster.setupMaster({
      exec: '3_worker.js'
      });

      if(cluster.isMaster) {
      for(let i = 0; i< cpus; i++) {
      cluster.fork();
      }
      cluster.on('fork', worker => console.log(worker.id + " is forked"));
      cluster.on('listening', (worker, address) => console.log(worker.id + "is listening on " + JSON.stringify(address)));
      cluster.on('online', worker => console.log(`${worker.id} is online`));
      cluster.on('disconnect', worker => console.log(`${worker.id} is disconnected`));
      cluster.on('exit', (worker, code, signal) => console.log(`${worker.id} is dead due to ${code} and ${signal}`));
      }


      3_worker.js



          const http = require('http');

      http.createServer((req, res) => {
      console.log(req.url);
      res.writeHead(200);
      res.end('hello');
      }).listen(8080);


      The above programs return result -



          3is listening on {"addressType":4,"address":null,"port":8080}
      5is listening on {"addressType":4,"address":null,"port":8080}
      4is listening on {"addressType":4,"address":null,"port":8080}
      7is listening on {"addressType":4,"address":null,"port":8080}
      8is listening on {"addressType":4,"address":null,"port":8080}
      2is listening on {"addressType":4,"address":null,"port":8080}
      1is listening on {"addressType":4,"address":null,"port":8080}
      6is listening on {"addressType":4,"address":null,"port":8080}


      The output of ps is -



          $ ps
      PID TTY TIME CMD
      4477 ttys001 0:00.16 /bin/bash -l
      5219 ttys001 0:00.12 node 3_clustering.js
      5220 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5221 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5222 ttys001 0:00.13 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5223 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5224 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5225 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5226 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5227 ttys001 0:00.14 /Users/rajkumar.natarajan/.nvm/versions/node/v8.15.0/b
      5794 ttys002 0:00.05 /bin/bash --rcfile /Applications/IntelliJ IDEA.app/Con
      6063 ttys003 0:00.03 /Applications/Utilities/iTerm.app/Contents/MacOS/iTerm
      6065 ttys003 0:00.04 -bash


      How multiple node.js process are able to listen on same port.







      node.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 27 '18 at 19:57









      Rajkumar Natarajan

      1,3171237




      1,3171237
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Only the master has the port opened. It handles all incoming and outgoing traffic on the port and offloads the traffic to the worker threads for processing.






          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',
            autoActivateHeartbeat: false,
            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%2f53950229%2fhow-port-is-reused-by-multiple-node-application-process%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Only the master has the port opened. It handles all incoming and outgoing traffic on the port and offloads the traffic to the worker threads for processing.






            share|improve this answer


























              2














              Only the master has the port opened. It handles all incoming and outgoing traffic on the port and offloads the traffic to the worker threads for processing.






              share|improve this answer
























                2












                2








                2






                Only the master has the port opened. It handles all incoming and outgoing traffic on the port and offloads the traffic to the worker threads for processing.






                share|improve this answer












                Only the master has the port opened. It handles all incoming and outgoing traffic on the port and offloads the traffic to the worker threads for processing.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 27 '18 at 21:03









                PeterVC

                4,31021113




                4,31021113






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53950229%2fhow-port-is-reused-by-multiple-node-application-process%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas