How port is reused by multiple node application process
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
add a comment |
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
add a comment |
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
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
node.js
asked Dec 27 '18 at 19:57
Rajkumar Natarajan
1,3171237
1,3171237
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 27 '18 at 21:03
PeterVC
4,31021113
4,31021113
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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