<
home PYTHONJAVA
 

Node.js Net


Node.js provides the ability to perform socket programming. We can create chat application or communicate client and server applications using socket programming in Node.js. The Node.js net module contains functions for creating both servers and clients.

Node.js Net Example

In this example, we are using two command prompts:

  • Node.js command prompt for server.
  • Window's default command prompt for client.

server:

File: net_server.js

  1. const net = require('net');  
  2. var server = net.createServer((socket) => {  
  3.   socket.end('goodbye\n');  
  4. }).on('error', (err) => {  
  5.   // handle errors here  
  6.   throw err;  
  7. });  
  8. // grab a random port.  
  9. server.listen(() => {  
  10.   address = server.address();  
  11.   console.log('opened server on %j', address);  
  12. });  





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.