Learn about the OSI seven layer model
osi layer |
features |
TCP/IP protocol |
Application layer |
file transfer, e-mail, file service, virtual terminal |
tftp,http,snmp,ftp,smtp, Dns,telnet |
presentation layer |
data formatting, transcoding, data encryption |
- |
Session layer |
data formatting, transcoding, data encryption |
- |
Transport layer |
|
tcp,udp |
Network layer |
Select route for packet |
ip,icmp,rip,ospf,bgp,igmp |
Data link layer |
transmit address frames and error detection |
slip,cslip,ppp,arp,rarp, MTU |
Physical layer |
transfer data on physical media as binary data |
ISO2 110,ieee802,ieee802.2 |
Main content
- TCP (Transmission Control Protocol)
- UDP (User Packet protocol)
- HTTP (Hyper-Text Transfer Protocol)
- Websocket
- Network services and Security (Cryto TLS HTTPS)
TCP Features
- Connection-oriented protocols
- Need three handshake
TCP Service Practice (i)
To create a service side:
var net = require (' net '); var server = Net.createserver (socket) { ///New Connection Socket.on (' Data ', function (data) { Console.log (' The server receives a message from the client: ' + data.tostring ()); Socket.write (' Service-side response: Hello '); }); Socket.on (' End ', function () { console.log (' Server Disconnect ');}) ; Server.listen (8124, function () { console.log (' TCP service Creation ');});
TCP Service Practice (ii)
To create a client:
var net = require (' net '); var client = Net.connect ({port:8124}, function () { console.log (' Client connection succeeded '); Client.write (' Client initiates greetings: Hello ');}); Client.on (' Data ', function (data) { Console.log (' Client receives service-side message: ' + data.tostring ()); Client.end ();}); Client.on (' End ', function () { console.log (' client disconnects ');});
Events for TCP Services (i)
- Server events
- Listening:server.listen ()
- Connection:net.createServer ()
- Close:server.close ()
- Error
the practice of TCP Services (ii)
- Connection Events
- Data: One end executes write () and the other end triggers the event
- End: Disconnect at either end to trigger the event
- Connect: This event is used by the client to trigger when the socket server connection succeeds
- Drain: When the Write () is triggered at either end, the event is triggered by the current end
- Error: This event is triggered when an exception occurs
- Close: This event is triggered when the socket ends
- Timeout: Triggers the event when the connection is no longer active after a certain amount of time
TCP Summary
- Connection oriented (establish communication lines: build, use, release)
- Three-time handshake (the process of establishing the connection)
- Nodejs implementation (NET module)
UDP features
- No connection
- Unreliable information Services
- In the case of poor network, packet loss is serious
- You can send a message to the client and receive a message from the server.
- Usage scenarios: scenarios where packet loss is not required (audio, video, DNS service)
UDP Service Practice (i)To create a service side:
var Dgram = require (' Dgram '); var server = dgram.createsocket (' udp4 '); Server.on (' message ', function (msg, rinfo) { console.log (' Server access information: ' +msg+ ' from: ' +rinfo.address+ ': ' +rinfo.port ');}); Server.on (' Listening ', function () { var address = server.address (); Console.log (' Decent service in listening: ' +address.address+ ': ' +address.port);}); Server.bind (41234);
UDP Service Practice (ii)To create a client:
var Dgram = require (' Dgram '); var client = Dgram.createsocket (' udp4 '); var message = new Buffer (' I am the client's message '); Client.send (msg, 0, Message.length, 41234, ' 10.16.15.155 ', function (err, bytes) { Console.log (' Client send complete, close client '); Client.close ();});
UDP Socket Events
- Message: When a UDP socket listens for a NIC port, the event is triggered when the message is received, carrying the buffer object and the remote address information
- Listening: This event is triggered when UDP starts to listen
- Close: This event is triggered when the close () method is called
- Error: When an exception occurs, the event is triggered and, if not processed, causes the process to exit
UDP Summary
- For no connection (no need to establish a communication line, send the packet with the destination address to the line)
- Usage scenarios (Low packet loss requirements, IP, UDP protocol are not connected)
- Nodejs implementation (Dgram module)
HTTP Features
- Application layer protocol based on TCP
- Classic mode: b/S
- Well-known HTTP standard: RFC2616 (both the and IETF)
HTTP Service Practice (i)
Create server: var http = require (' http '); Http.createserver (function (req, res) { Res.writehead ($, {' Content-type ': ' Text/plain '}); Res.end (' Hello world!\n ');}). Listen (8000); Console.log (' Server enabled ');
HTTP Messages
- Part I: Classic TCP three-time handshake
- Part Two: Client (curl) Sending request message
- Part III: Server response (includes response header and response body)
- Part IV: Ending a session
HTTP Service Practice (ii)To create a client:
var http = require (' http '); var options = { hostname: ' 10.16.15.155 ', port:8000, method: ' GET ', path: '/'}, var req = Http.request (o ptions, Function (res) { console.log (' Status: ' +res.statuscode); Console.log (' Headers: ' +json.stringify (res.headers)); Res.setencoding (' Utf-8 '); Res.on (' Data ', function (chunk) { console.log (chunk); });}); Req.end ();
HTTP Service event (i)
- Server-side events
- Connection
- Request
- Close
- Checkcontinue
- Connect
- Upgrade
- Clienterror
HTTP Service Events (ii)
- Client Events
- Response
- Socket
- Connect
- Upgrade
- Continue
HTTP Summary
- HTTP protocol based on TCP
- Client requests can be done via impersonation (curl, http_client.js, browser, etc.)
- Nodejs implementation (HTTP module)
WebSocket features
- Built on top of HTTP service
- After the connection is established, it is upgrade to the data frame protocol, which realizes the interaction between the server and the client.
- Full Duplex communication
WebSocket Instances (socketio)Socketio Document Socketio Example
WebSocket Summary
- Full Duplex communication
- The protocol is switched after the HTTP service is created
- Nodejs implementation (WS, Socket.io module)
Network services and securityThe process of a secure connection:
- Certification based on digital certificates
- For encrypted transmissions
- Exchanging public keys
- Client encrypts using the public key of the service side
- The server uses the server-side private key to decrypt it.
- The server uses the client's public key for encryption
- Client uses the client's private key to decrypt
Network Programming Summary
- Understanding the OSI seven layer model
- Nodejs implementation of the Key Learning Transport layer (TCP, UDP) and Application layer (HTTP, Websocket) protocol
- Preliminary understanding of network transport security
Reference: "Nodejs in layman's language"--the seventh Chapter network programming
Network programming of Nodejs Learning notes