WebSocket, FTP, RESTful, SMTP, IMAP, POP and other protocols many people may have only heard of, but do not know about.

A, ARP/RARP/ICMP

Address Resolution Protocol (ARP) is an Address Resolution Protocol.

  • Obtain a MAC address from an IP address.

Reverse Address Resolution Protocol (RARP) : Reverse Address Resolution Protocol.

  • Use the same header structure as ARP
  • ARP is used to translate a MAC address into an IP address
  • Later, BOOTP and DHCP were replaced

Internet Control Message Protocol (ICMP) is an Internet Control Message Protocol.

  • ICMP in IPV4 is called ICMPv4, and ICMP in IPV6 is called ICMPv6
  • Usually used to return error information, such as TTL value expired, destination unreachable
  • ICMP error messages always contain the source data and are returned to the sender

Second, the WebSocket

WebSocket is an application layer protocol based on TCP that supports full-duplex communication (full-duplex: both the client and the server can actively send messages to each other). It was standardised by THE IETF in 2011 as RFC_6455, later supplemented by RFC_7936.

WebSocket is used in many scenarios: social subscriptions, stock fund quotes, sports updates, multimedia chat, multi-player games, etc.

Socket is a set of network programming API, almost all network application protocols (HTTP, FTP, SMTP, etc.) are implemented based on Socket. WebSocket is an application layer communication protocol on the Web side.

2.1. HTTP and WebSocket

HTTP request features: communication can only be initiated by the client, so early many websites in order to achieve push technology, the use of technology is polling.

Polling means that the browser makes HTTP requests to the server at regular intervals (e.g., every second), and the server returns the latest data to the client. In order to better save server resources and bandwidth, and can more real-time communication, HTML5 specification appeared WebSocket protocol.

WebSocket and HTTP are both application layer protocols. In fact, TCP itself supports full-duplex communication, but HTTP’s request-reply mode limits TCP’s capability.

Websockets use ports 80 (ws://) and 443 (WSS ://), which can bypass most firewall restrictions (HTTP uses port 80 by default, HTTPS uses port 443 by default).

Unlike HTTP, Websockets need to establish connections first (application-layer connections). This makes WebSocket a stateful protocol that can then communicate without some state information. HTTP requests may require additional status information (such as authentication) on each request.

2.2. Establish a connection

A WebSocket requires an HTTP Handshake.

The client (browser) initiates the handshake request:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Copy the code

Server response:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: chat
Copy the code

Connection: Upgrade must be set to indicate that the client wants to connect to the Upgrade.

Upgrade: WebSocket must be set to indicate that you want to Upgrade to the WebSocket protocol.

Sec-websocket-version: indicates the supported WebSocket Version. RFC_6455 requires Version 13.

Sec-websocket-key: random string generated by the client (such as dGhlIHNhbXBsZSBub25jZQ== in the example).

After receiving the sec-websocket-key from the client, the server performs the following operations:

  1. Sec-websocket-key concatenates a fixed GUID value (258eAFa5-E914-47DA-95CA-C5AB0DC85b11). Example: dGhlIHNhbXBsZSBub25jZQ = = 258 eafa5 – ca – C5AB0DC85B11 da E914-47-95.

  2. The results of 1 are summarized by sha-1

Results: b37a4f2cc0624f1690f64606cf385945b2bec4ea.

  1. Hex to Base64 encodes the result of 2.

  2. Return the result of 3 as the value of the sec-websocket-accept response header to the client.

In this way, ordinary HTTP requests are avoided from being mistaken for WebSocket protocols.

2.3. Use

WebSocket experience and presentation: www.websocket.org/echo.html

The W3C has standardized a WebSocket API that can be called directly using JS.

let ws = new WebSocket('wss://example.com')
Copy the code

Third, WebService

WebService (WebService) is a technical standard for remote invocation across programming languages and operating system platforms.

Examples of WebService usage scenarios:

  • Weather forecast, mobile phone home inquiries, flight information inquiries, logistics information inquiries, etc.
  • For example, weather forecast, is the weather bureau to expose their services in the form of WebService, so that third-party programs can call these service functions (www.webxml.com.cn/zh_cn/index…

In fact, WebServices can be replaced by plain Web apis (such as HTTP + JSON), and many enterprise open platforms now use Web APIS directly.

3.1. WebService – Core Concept

Simple Object Access Protocol (SOAP). Most of the time, SOAP = HTTP + XML. Webservices use the SOAP protocol to encapsulate and deliver data.

SOAP is just a normal HTTP request and formalized XML. The request and response are XML files.

Web Services Description Language (WSDL) : Web Services Description Language. An XML document that describes the details of the WebService interface (such as parameters, return values, and so on). Usually follow the URL of the WebService, right? WSDL Gets WSDL information. Such as: ws.webxml.com.cn/WebServices…

Four, RESTful

Representatioanl State Transfer (REST) represents layer State Transfer.

REST is an Internet software architecture design style that defines a set of constraints for creating Web services. Web services that conform to the REST architecture are called RESTful Web servers.

Ful after RESTful means an adjective, such as wonder -> wonderful.

Practical suggestions:

  • Use nouns in urls (plural is recommended), not verbs.

    • Recommendation:/users,/users/6
    • Is not recommended:listUsers,/getUser? id=6,/user/list,/user/get? id=6
  • Use HTTP to express actions:

  • A resource is connected to another resource as a subresource

    • The GET:/users/6/cars/88
    • POST:/users/8/cars
  • API version

    • idbeny.com/v1/users
    • idbeny.com/v2/users/66
  • Returns data in JSON format

  • When an error occurs, do not return the 200 status code

Fifth, HTTPDNS

The HTTPDNS sends domain name resolution requests to the DNS server based on the HTTP protocol. It replaces the traditional method of sending resolution requests to the Local DNS of carriers based on THE DNS protocol, avoiding domain name hijacking and cross-network access problems caused by the Local DNS. Commonly used in mobile Internet (such as Android, iOS development).

There are already ready-made solutions for mobile integration with the SDK to use the HTTPDNS service.

  • Tencent cloud: cloud.tencent.com/product/htt…
  • Ali cloud: help.aliyun.com/product/301…

Six, FTP

File Transport Protocol (FTP) : File transfer Protocol. RFC_959, which defines this specification, is a TCP-based application layer protocol. As defined in RFC_1738, the FTP URL format is ftp://[user[:password]@]host:[:port]/url-path.

6.1. Connection mode

FTP connection modes are Active and Passive.

Either way, two connections are required between the client and the server:

  • Control connection: used to transmit status information (instructions)
  • Data connection: used to transfer file and directory information (data)

6.1.1. Active mode

In active mode, the server is active and the client is passive.

  • The client opens a random command port
    • The port number is greater than 1024, for example, N. Simultaneous connection to server command port 21 (fixed)
  • The client starts to listen on the N+1 data port
    • Also send a Port command to the server’s command Port 21
    • This command tells the server that the data port the client is listening for is N+1 and that it is ready to receive data from this port
  • The server opens data port 20 (fixed) and creates a connection to client data port N+1

6.1.2. Passive mode

In passive mode, the client is active and the server is passive.

The client connects to the server through two random ports: command port N and data port N+1.

  • Client command port N Command port 21 for connecting to the server (fixed)
  • The client sends the PASV command through command port N to command port 21 of the server
  • The server opens a random data port P and tells the client that the port number is P
  • Client data port N+1 initiates a connection with server data port P

7. Mail related agreements

  • Protocol used for sending emails:

    • SMTP (Simple Mail Transfer Protocol), simple Mail Transfer protocol.
      • Based on TCP, the standard reference is RFC_5321
      • Port 25 is used by default, and port 465 is used for SSL/TLS
  • Protocol used for receiving mail:

    • Post Office Protocol (POP) : Post Office Protocol.

      • Based on TCP, the latest version is POP3, standard reference RFC_1939
      • Port 110 is used by default, and port 995 is used for SSL/TLS
    • Internet Message Access Protocol (IMAP) is an Internet information Access Protocol.

      • Based on TCP, the latest version is IMAP4, standard reference RFC_3501
      • By default, the server uses port 143 and port 993 for SSL/TLS

7.1. Process of sending and receiving emails

For example, if Tencent Email (@qq.com) wants to send emails to netease email (@163.com), the client sends emails to Tencent email service through SMTP, and the Tencent email server forwards emails to netease email server through SMTP. Netease Mailbox users can pull and retrieve emails from the netease mailbox server through the POP/IMAP protocol.

7.2. Characteristics of POP

  • When the client connects to the server, all messages are downloaded from the server.
    • You can set the server mail to be deleted immediately after downloading or after a period of time.
  • Client-side operations, such as deleting messages or moving to folders, are not synchronized with the server.
  • Each client is independent and can get its own copy of the E-mail.

7.3. Features of IMAP

  • When the client connects to the server, it obtains the basic information about the mail on the server and does not download the mail.
    • Wait until the message is opened before downloading it.
  • Client-side actions, such as deleting emails or moving to folders, are synchronized with the server.
  • All clients always see the same message and the same folder.

For more articles in this series, please pay attention to wechat official account [1024 Planet].