This paper is participating in theNetwork protocols must be known and must be known”Essay campaign
🌑 o
introduceOSI
Seven layer model
🕛 O. 1 Physical layerPhysical
The Physical Layer sends Data frames on the LAN and manages the communication between computer communication devices and network media. It includes pins, voltages, cable specifications, hubs, Repeaters, network cards, host interface cards, and so on.
Picture 1: Two computers communicate through a single switch
In Figure 1, network cables and FIBRE Channel switches belong to the physical layer
🕧 O. 2 Data link layerData Link layer
The Data Link Layer is responsible for network addressing, error detection, and error correction. When the header and tail of a table are added to a Data packet, an information Frame is formed. The data link header (DLH) contains physical addresses and error detection and correction methods. A data linked list tail (DLT) is a string indicating the end of a packet. For example, Ethernet, WIRELESS LAN Wi-Fi and general packet wireless service GPRS.
It is divided into two sub-layers: logical Link Control, LLC sub-layer and Media Access control, MAC sub-layer.
🕐 O. 3 Network layerNetwork layer
The Network Layer determines the path selection and forwarding of data, and adds the Network table header NH to packets to form packets. The network table header contains network data. For example, Internet protocol IP.
🕜 O. 4 Transport layerTransport layer
The Transport Layer adds the Transport header TH to the data to form a group. The transport header contains the protocol used to send information. For example, transmission control protocol TCP.
🕑 O. 5 Session layerSession layer
The Session Layer is responsible for setting up and maintaining communication connections between two computers in the computer network during data transmission.
🕝 O. 6 Presentation layerPresentation layer
The Presentation Layer converts data into a format that is compatible with the receiver’s system format and suitable for transmission.
🕒 O. 7 Application layerApplication layer
The Application Layer provides an interface designed for an Application to set up communication with another Application. For example, HTTP HTTPS FTP Telnet SSH SMTP POP3.
🌒 1
What is thesocket
socket
An interprocess communication mechanism provided by the operating system.
In operating systems, applications are typically provided with a set of application program interface apis called socket apis. Applications can use network sockets through socket interfaces for data exchange. The earliest socket interfaces came from 4.2BSD, so most common modern sockets are derived from the Berkeley socket standard. In a socket interface, an IP address and a port form the socket address. The remote and local socket addresses are wired together with the protocol used, the five-element tuple, as socket pair pairs, to exchange data with each other. For example, TCP and UDP can use the same port on the same computer without interfering with each other. Based on the socket address, the operating system can determine which particular travel or thread the data should be delivered to.
🌓 2
TCP
- Protocol is introduced
Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream – based transport layer Protocol. In the simplified OSI model of computer networks, it performs the functions specified by the fourth transport layer. UDP is another important transport protocol in the same layer.
- TCP server
Server function
1 Listens for port 2 to receive the client connection request. 3 processes the messageCopy the code
server.go
package server
import (
"bufio"
"fmt"
"net"
)
func process(conn net.Conn) {
defer conn.Close() // Close the connection
for {
reader := bufio.NewReader(conn)
var buf [128]byte
n, err := reader.Read(buf[:]) // Read data
iferr ! =nil {
fmt.Println("read from client failed, err:", err)
break
}
recvStr := string(buf[:n])
fmt.Println("Received data from client:", recvStr)
conn.Write([]byte(recvStr)) // Send data}}func main(a) {
listen, err := net.Listen("tcp"."127.0.0.1:8080")
iferr ! =nil {
fmt.Println("listen failed, err:", err)
return
}
for {
conn, err := listen.Accept() // Establish a connection
iferr ! =nil {
fmt.Println("accept failed, err:", err)
continue
}
go process(conn) // Start a Goroutine processing connection}}Copy the code
- TCP client
Client function
1 Establish a connection with the server. 2 Send and receive data. 3 Close the connectionCopy the code
client.go
package server
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
// tcp/client/main.go
/ / the client
func main(a) {
conn, err := net.Dial("tcp"."127.0.0.1:8080")
iferr ! =nil {
fmt.Println("err :", err)
return
}
defer conn.Close() // Close the connection
inputReader := bufio.NewReader(os.Stdin)
for {
input, _ := inputReader.ReadString('\n') // Read user input
inputInfo := strings.Trim(input, "\r\n")
if strings.ToUpper(inputInfo) == "Q" { // Exit if you enter q
return
}
_, err = conn.Write([]byte(inputInfo)) // Send data
iferr ! =nil {
return
}
buf := [512]byte{}
n, err := conn.Read(buf[:])
iferr ! =nil {
fmt.Println("recv failed, err:", err)
return
}
fmt.Println(string(buf[:n]))
}
}
Copy the code
- messaging
🌕 3
UDP
- Protocol is introduced
User Datagram Protocol (UDP) is a simple packet – oriented communication Protocol at the transport layer of OSI model.
- Udp server.
server.go
package main
import (
"fmt"
"net"
)
// UDP/server/main.go
/ / UDP server side
func main(a) {
listen, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IPv4(0.0.0.0),
Port: 30000,})iferr ! =nil {
fmt.Println("listen failed, err:", err)
return
}
defer listen.Close()
for {
var data [1024]byte
n, addr, err := listen.ReadFromUDP(data[:]) // Receive data
iferr ! =nil {
fmt.Println("read udp failed, err:", err)
continue
}
fmt.Printf("data:%v addr:%v count:%v\n".string(data[:n]), addr, n)
_, err = listen.WriteToUDP(data[:n], addr) // Send data
iferr ! =nil {
fmt.Println("write to udp failed, err:", err)
continue}}}Copy the code
- Udp client
package main
import (
"fmt"
"net"
)
// UDP client
func main(a) {
socket, err := net.DialUDP("udp".nil, &net.UDPAddr{
IP: net.IPv4(0.0.0.0),
Port: 30000,})iferr ! =nil {
fmt.Println("Failed to connect to server, err:", err)
return
}
defer socket.Close()
sendData := []byte("Hello server")
_, err = socket.Write(sendData) // Send data
iferr ! =nil {
fmt.Println("Failed to send data, err:", err)
return
}
data := make([]byte.4096)
n, remoteAddr, err := socket.ReadFromUDP(data) // Receive data
iferr ! =nil {
fmt.Println("Failed to receive data, err:", err)
return
}
fmt.Printf("recv:%v addr:%v count:%v\n".string(data[:n]), remoteAddr, n)
}
Copy the code
- messaging