Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

net

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

net

Globalizes the 'net' module functions

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is net?

The 'net' npm package provides an asynchronous network API for creating stream-based TCP or IPC servers and clients. It is a core module in Node.js, allowing developers to build network applications with ease.

What are net's main functionalities?

Creating a TCP Server

This feature allows you to create a TCP server that listens for incoming connections on a specified port and IP address. The server can send and receive data from connected clients.

const net = require('net');
const server = net.createServer((socket) => {
  socket.write('Hello from server!\n');
  socket.on('data', (data) => {
    console.log('Received:', data.toString());
  });
});
server.listen(8080, '127.0.0.1', () => {
  console.log('Server listening on port 8080');
});

Creating a TCP Client

This feature allows you to create a TCP client that connects to a specified server. The client can send and receive data from the server.

const net = require('net');
const client = net.createConnection({ port: 8080, host: '127.0.0.1' }, () => {
  console.log('Connected to server!');
  client.write('Hello from client!');
});
client.on('data', (data) => {
  console.log('Received:', data.toString());
  client.end();
});
client.on('end', () => {
  console.log('Disconnected from server');
});

Handling Multiple Connections

This feature demonstrates how to handle multiple client connections on a TCP server. Each new connection is logged, and data can be sent and received from each client independently.

const net = require('net');
const server = net.createServer((socket) => {
  console.log('New connection');
  socket.write('Welcome!\n');
  socket.on('data', (data) => {
    console.log('Received:', data.toString());
  });
  socket.on('end', () => {
    console.log('Connection closed');
  });
});
server.listen(8080, '127.0.0.1', () => {
  console.log('Server listening on port 8080');
});

Other packages similar to net

FAQs

Package last updated on 19 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc