[GO language foundation] GO network programming, realization of TCP/IP model (10)

network programming

  1. TCP socket programming is the mainstream of network programming. It is called Tcp socket programming because the bottom layer is based on the Tcp/ip protocol.
  2. For http programming with b/s structure, when we use a browser to access the server, we use the http protocol, and the bottom layer of http is still implemented with tcp socket.

Protocol (tcp/ip)

The abbreviation of TCP/IP (Transmission Control Protocol/Internet Protocol), the Chinese translation is called Transmission Control Protocol/Internet Protocol, also known as Network Communication Protocol. This protocol is the most basic protocol of the Internet and the foundation of the Internet International Internet Network. Simply put , Is composed of the IP protocol of the network layer and the TCP protocol of the transport layer.

OSI Reference Model (Theory)

Insert picture description here

  • Application layer: The layer closest to the user in the OSI reference model is to provide computer users with application interfaces and directly provide users with various network services. Our common application layer network service protocols are: HTTP, HTTPS, FTP, POP3, SMTP, etc.

  • Presentation layer: The presentation layer provides various coding and conversion functions for application layer data to ensure that the data sent by the application layer of one system can be recognized by the application layer of another system. If necessary, this layer can provide a standard representation for converting multiple data formats inside the computer into the standard representation used in communications. Data compression and encryption are also one of the conversion functions that the presentation layer can provide.

  • Session layer: The session layer is responsible for establishing, managing, and terminating communication sessions between presentation layer entities. The communication at this layer consists of service requests and responses between applications in different devices.

  • Transport layer: The transport layer establishes an end-to-end link from the host. The role of the transport layer is to provide end-to-end reliable and transparent data transmission services for upper-layer protocols, including handling error control and flow control issues. This layer shields the details of the lower-layer data communication from the upper layer, so that the upper-layer users see only a host-to-host, reliable data path between the two transmission entities that can be controlled and set by the user. We usually say that TCP UDP is at this layer. The port number is the "end" here.

  • Network layer: This layer establishes a connection between two nodes through IP addressing, selects appropriate routing and switching nodes for the packets sent from the transport layer at the source end, and transmits them to the transport layer at the destination end according to the address correctly. It is usually referred to as the IP layer. This layer is what we often call the IP protocol layer. The IP protocol is the foundation of the Internet.

  • Data link layer: Combine bits into bytes, then combine bytes into frames, use link layer addresses (Ethernet uses MAC addresses) to access the medium, and perform error detection.

    The data link layer is divided into 2 sublayers: logical link control sublayer (LLC) and media access control sublayer (MAC).
    The MAC sublayer handles CSMA/CD algorithms, data error checking, framing, etc.; the LLC sublayer defines some fields so that the previous protocol can share the data link layer. In actual use, the LLC sub-layer is not necessary.

    The mac address is solidified in the ROM of the network card, 48 bits, hexadecimal, the first 24 bits are the manufacturer code, and the last 24 bits are the serial number.
    Windows check the local mac address, ipconfig /all, the physical address is the local mac address: F4-8C-50-31-80-99, a hexadecimal digit is 4 binary digits.

  • Physical layer: The actual final signal transmission is realized through the physical layer. The bit stream is transmitted through the physical medium. The level, speed, and cable pin are specified. Commonly used devices are (various physical devices) hubs, repeaters, modems, network cables, twisted pairs, and coaxial cables. These are the transmission media of the physical layer.

TCP/IP model (realistic)

  • Correspondence between TCP/IP five-layer protocol and OSI seven-layer protocol

Insert picture description here

  • Different equipment is working on each floor
  1. Physical layer equipment, shielded twisted pair, unshielded twisted pair, hub, converter, repeater. Both converters and modulators are used for digital/analog conversion. The repeater is used to amplify the transmission signal. The hub is to connect multiple computers together to form a local area network. When any device connected to the hub sends data, all other devices must wait, because the hub uses a bus-type network structure; there is no operating system inside the hub, and no mac address cache, so it cannot determine the destination of the data packet , So it sends the data packet to each device in a broadcast manner, and the receiver judges whether it is sent to itself according to the mac information in the data packet, not lost; as the price of the switch decreases, hubs are no longer common.
  2. Data link layer equipment, switches, bridges, network cards. Network bridges are not common anymore. Network bridges are switches with few ports, and switches are network bridges with many ports. Switch, error checking, error frames will not be forwarded. The switch sends data to a port. The first time it sends a broadcast, because it has an operating system and has learning capabilities, it will cache the mac address of this port after the first time, so that when it is sent to this device again, The broadcast will not be sent.
  3. Network layer equipment, routers. The router itself has a three-layer structure, the physical layer, the data link layer, and the network layer.
  4. The firewall works at the network layer and the transport layer, and it performs network access control according to the network policy set by the administrator. There are hardware and software firewalls; hardware is expensive.

The switch does not recognize the IP, the switch can only communicate through the mac address, and can only communicate within the local area network. Once the network segment is crossed, it cannot communicate through the switch.

To communicate across network segments through routers.

Gateway, the exit of the entire LAN. Data packets that cannot be processed in the local area network are handed over to the gateway for processing. If communicating with the external network, the source mac is the sender's mac, and the target mac is the gateway of the local area network. If it is an intranet communication to an external network, because the internal IP cannot access the external network, when the data packet is sent to the gateway, the gateway will wrap its own public IP outside the internal IP, which is called NAT network address translation. If it is a public network communication external network, there will be no NAT network address translation. After multiple routes, the IP does not change anymore.

When transmitting data in the network, the source mac is always changing, and it will change as long as it passes through a layer of routing.

Insert picture description here

  • The protocols implemented at each layer are also different, that is, the services of each layer are also different.

The domain name resolution system DNS uses both UDP and TCP. The DNS server has master and slave servers, and TCP is used when synchronizing data between them; when we enter the domain name in the browser, it needs to be resolved to an IP address, and UDP is used at this time.

In the LAN, user A is connected to user B through IP; however, the switch does not recognize the IP, but there is a table corresponding to the mac and ip of all computers in the local area network. The arp protocol is a protocol that converts the internal network ip to a mac address when communicating in a local area network.

In the local area network, windows enter arp -a on the command line to view the correspondence between mac and ip in the local area network.

The data link layer is very vague, and it is difficult to distinguish which layer it belongs to.

Insert picture description here

  • Data encapsulation process
Model level data structure
Application layer Upper-level data
Transport layer tcp header upper layer data
Network layer ip header tcp header upper layer data
data link layer mac header ip header tcp header upper layer data
  • ip and port
  1. Every host and router on the internet has an ip address, which includes the network number and the host number. The address can be ipv4 (32ip bit) or ipv6 (128 bit). You can view it through ipconfig
  2. Port
    0 号是保留端口
    1-1024 是固定端口(程序员不要使用),又叫有名端口,即被某些程序固定使用,一般程序员不使用.
        22: SSH 远程登录协议 23: telnet 使用
        25: smtp 服务使用
        21: ftp 使用
        80: iis 使用 7: echo 服务
    1025-65535 是动态端口,这些端口,程序员可以使用.

Use attention

  1. Open as few ports as possible on the computer (especially as a server)
  2. A port can only be monitored by one program
  3. If you use netstat -an, you can check which ports are listening on this machine
  4. You can use netstat -anb to view the pid of the listening port, and close the insecure port in conjunction with the task manager

server.go

    package main
    import (
        "fmt"
        "net" //做网络socket开发时,net包含有我们需要所有的方法和函数
        _"io"
    )

    func process(conn net.Conn) {

        //这里我们循环的接收客户端发送的数据
        defer conn.Close() //关闭conn

        for {
            //创建一个新的切片
            buf := make([]byte, 1024)
            //conn.Read(buf)
            //1. 等待客户端通过conn发送信息
            //2. 如果客户端没有wrtie[发送],那么协程就阻塞在这里
            //fmt.Printf("服务器在等待客户端%s 发送信息\n", conn.RemoteAddr().String())
            n , err := conn.Read(buf) //从conn读取
            if err != nil {
                
                fmt.Printf("客户端退出 err=%v", err)
                return //!!!
            }
            //3. 显示客户端发送的内容到服务器的终端
            fmt.Print(string(buf[:n])) 
        }
    }

    func main() {
        fmt.Println("服务器开始监听....")
        //net.Listen("tcp", "0.0.0.0:8888")
        //1. tcp 表示使用网络协议是tcp
        //2. 0.0.0.0:8888 表示在本地监听 8888端口
        listen, err := net.Listen("tcp", "0.0.0.0:8888")
        if err != nil {
            fmt.Println("listen err=", err)
            return 
        }
        defer listen.Close() //延时关闭listen

        //循环等待客户端来链接我
        for {
            //等待客户端链接
            fmt.Println("等待客户端来链接....")
            conn, err := listen.Accept()
            if err != nil {
                fmt.Println("Accept() err=", err)
                
            } else {
                fmt.Printf("Accept() suc con=%v 客户端ip=%v\n", conn, conn.RemoteAddr().String())
            }
            //这里准备其一个协程,为客户端服务
            go process(conn)
        }
    }

client.go

    package main
    import (
        "fmt"
        "net"
        "bufio"
        "os"
        "strings"
    )

    func main() {
        conn, err := net.Dial("tcp", "192.168.20.253:8888")
        if err != nil {
            fmt.Println("client dial err=", err)
            return 
        }
        //功能一:客户端可以发送单行数据,然后就退出
        reader := bufio.NewReader(os.Stdin) //os.Stdin 代表标准输入[终端]

        for {
            //从终端读取一行用户输入,并准备发送给服务器
            line, err := reader.ReadString('\n')
            if err != nil {
                fmt.Println("readString err=", err)
            }
            //如果用户输入的是 exit就退出
            line = strings.Trim(line, " \r\n")
            if line == "exit" {
                fmt.Println("客户端退出..")
                break
            }

            //再将line 发送给 服务器
            _, err = conn.Write([]byte(line + "\n"))
            if err != nil {
                fmt.Println("conn.Write err=", err)	
            }
        }
    }

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/114005941