首页 > 其他分享 >Sockets / net Module Review

Sockets / net Module Review

时间:2023-02-13 07:11:28浏览次数:39  
标签:const Review sock server net Server data Sockets

Node's net Module

What's the net module used for? →

It's for creating TCP/IP servers and clients.

What are the objects that we used when creating a server? →

  • a Server object (of course!) … which binds to a hostname and port, and accepts connections and data from clients
  • a Socket object … that allows reading and writing data to a specific connection
 

Creating a Server

So, how do we create a server? →

  • easy - just call createServer!!!!
  • createServer gets a callback function, though… what is this callback for? →
  • it's the function that's called when a client connects to the server… the callback gets a socket passed to it as an argument… which can be used to read and write data to the client
  • here's a simple example:
const net = require('net');
const server = net.createServer(function(sock) {
    console.log('Got connection from (addr, port):', sock.remoteAddress, sock.remotePort); 
});
server.listen(8080, '127.0.0.1');
 

The Socket Object

On every connection: →

  • the callback passed to createServer gets called with an instance a Socket object
  • you can think of that socket object as our interface to the connection to the client
  • what can the socket object do? →
    • respond to data events (that is, read data…)
    • respond to close events (listen for a closed connection)
    • write data to the client
  • note that events work by calling on and sending it a callback that determines what to do on that event
 

An Echo Server Example

The classic hello world for network programming is an echo serverHere's our version. →

 sock.on('data', function(binaryData) {
    console.log('got data\n=====\n' + binaryData); 
    sock.write(binaryData);

    // uncomment me if you want the connection to close
    // immediately after we send back data
    // sock.end();
});
 

Echo Server → Web Server

So… if we try pointing our browser to our echo server, not much happens. Our server doesn't "speak HTTP" yetWhat do we have to do if we want to turn our echo server into a web server that responds to different paths by sending back html documents? →

  • treat the incoming data as http, and parse out the path
  • send back valid http responses with an html document as the body

Web Server - Setup

Aaand… we did just that! During our live coding demo, we came up with this code… →

First, some setup:

const net = require('net');

// Request object goes here...

const server = net.createServer(function(sock) {
    // "routing" goes here
});
server.listen(8080, '127.0.0.1');
 

Web Server - Request Object

Then, a Request object to parse out the path: →

class Request {
    constructor(s) {
        const requestParts = s.split(' ');
        const path = requestParts[1];
        this.path = path;
    }
}
 

Web Server - Routing / Content

Now let's do routing and serving content all in one shot! →

On connection …

console.log('connected', sock.remoteAddress, sock.remotePort);
sock.on('data', (binaryData) => {
    const reqString = '' + binaryData; 
    const req = new Request(reqString);
    if(req.path === '/about') {
        sock.write('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<h2>hello</h2>');
    } else  if(req.path === '/test') {
        sock.write('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<h2>test</h2>');
    }         
    sock.end();
});

标签:const,Review,sock,server,net,Server,data,Sockets
From: https://www.cnblogs.com/M1stF0rest/p/17115176.html

相关文章