precondition

  1. The Node environment needs to be installed

Create a project

  1. Creating a server file
Create server file and enter mkdir server && CD serverCopy the code

Creating a Server

  1. Create a server. Js
Const net = require("net"); // Port number and address const port = 3000; Const host = "127.0.0.1"; const server = net.createServer((socket) => { // 2. Log ("2. Server is connected to" + socket); }); Listen (port, host, () => {console.log("1. server is running"); });Copy the code
  1. Vscode run server. Js
// CTRL + 'opens terminal Node server.jsCopy the code
  1. Output result
PS E:\node\the_server> node.\ server.js 1Copy the code

Creating a Client

  1. Create client. Js
Const net = require("net"); const port = 3000; Const host = "127.0.0.1"; Const client = net.createconnection (port, host, port); // createConnection const client = net.createconnection (port, host, port); On ("connect", () => {console.log("1. });Copy the code
  1. Vscode running client. Js
// CTRL + Shift + 'Create a terminal node client.jsCopy the code
  1. Output result
PS E:\node\the_server> node.\ client.js 1. Client is connected // Successfully connected to server.jsCopy the code
  1. When a connection to server.js succeeds, the waiting connection to server.js also receives a response
PS E:\node\the_server> node .\server.js 1. server is running 2. server is connected to[object Object] // The socket is a Buffer objectCopy the code

At this point, the connection between the server and client is complete.

To transfer data

The next step is passing the data

  1. The service side server. Js
Const net = require("net"); const port = 3000; Const host = "127.0.0.1"; Const server = net.createserver ((socket) => {// 2. Log ("2. Server is connected to" + socket); Socket. On ("data", (data) => {console.log("3. Server get client data: ", data.tostring ()); // Write and return socket.write("this is server"); }); }); Listen (port, host, () => {console.log("1. server is running"); });Copy the code
  1. The client client. Js
Const net = require("net"); const port = 3000; Const host = "127.0.0.1"; Const client = net.createconnection (port, host, port); // createConnection const client = net.createconnection (port, host, port); On ("connect", () => {console.log("1. }); Buffer client. On ("data", (data) => {// 3. Console. log("2. Client get server data:", data.tostring ()); }); client.end("this is client");Copy the code
  1. The output
// server.js PS G:\vue-study\Node\server> node .\server.js 1. server is running 2. server is connected to[object Object]  3. server get client data: this is clientCopy the code
// client.js
PS G:\vue-study\Node\server> node .\client.js
1. client is connected
2. client get server data: this is server
Copy the code
  1. use.endor.writeCan write data

Close the service

  1. server.js
Const net = require("net"); const port = 3000; Const host = "127.0.0.1"; const server = net.createServer((socket) => { // 2. Log ("2. Server is connected to" + socket); Socket. On ("data", (data) => {console.log("3. Server get client data: ", data.tostring ()); // Write and return socket.write("this is server"); }); On ("end", function () {console.log("4. Client closes the connection "); }); On ("close", () => {console.log("5. Server is close"); }); }); Listen (port, host, () => {console.log("1. server is running"); });Copy the code
  1. client.js
Const net = require("net"); const port = 3000; Const host = "127.0.0.1"; Const client = net.createconnection (port, host, port); // createConnection const client = net.createconnection (port, host, port); On ("connect", () => {console.log("1. }); Buffer client. On ("data", (data) => {// 3. Console. log("2. Client get server data:", data.tostring ()); }); client.end("this is client"); client.on("close", () => { console.log("3. client is closed"); });Copy the code
  1. The output
// server.js PS G:\vue-study\Node\server> node .\server.js 1. server is running 2. server is connected to[object Object]  3. server get client data: this is client 4. 5. The server is closeCopy the code
// client.js
PS G:\vue-study\Node\server> node .\client.js
1. client is connected
2. client get server data: this is server
3. client is closed
PS G:\vue-study\Node\server>
Copy the code

conclusion

  1. netThe module is a built-in Node module that provides an asynchronous network API for creating stream-based TCP or IPC servers (net.createServer()) and client (net.createConnection()).
  2. So far we have implemented a simple full-duplex communication.