crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Node.js Net
Question Posted on 04 Feb 2024Home >> Tutorial >> Node.js Tutorial >> Node.js Net

Node.js Net

Node.js Net



Well the Node.js will provides the ability to perform the socket programming. And we can create chat application or communicate client and server application by use of the socket programming in Node.js. And when we say about the Net module in Node.js, this will contains functions for both clients and servers creation.

Node.js Net Example



Here in this example we us two command prompt:-

(1)Node.js command prompt for server
(2)Windows's default command prompt for client.

server:

File: netserver.js


const net = require('net');
var server = net.createServer((socket) => {
socket.end('goodbye\n');
}).on('error', (err) => {
// handle errors here
throw err;
});
// grab a random port.
server.listen(() => {
address = server.address();
console.log('opened server on %j', address);
});




Now open Node.js command prompt to run the below coe to execute .js file


node net_server.js








Node.js Server

Now we will jump to client code

client:

File: netclient.js


const net = require('net');
const client = net.connect({port: 50302}, () => {//use same port of server
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});



Now open Node.js command prompt to run the below coe to execute .js file


node netclient.js




serverclientNodeJS


Node.js Client
0
0



.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

Parts of Node.js

Install Node.js Linux

Node.js First Example

Node.js Globals

Node.js DNS






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap