Let's talk about network programming

Let's talk about network programming

Introduction

【1】Network programming:

Computers distributed in different geographical areas and specialized external devices are interconnected through communication lines to form a large-scale, powerful network system, so that numerous computers can easily transmit information to each other and share hardware, software, data information and other resources.

Devices transmit and receive data in the network.

【2】Two important elements of communication: IP+PORT

【3】When transmitting between devices, certain rules must be followed---》Communication protocol:

【4】TCP protocol: reliable

Establishing a connection: three-way handshake

Release the connection: wave four times

【5】UDP protocol: unreliable

Network communication based on TCP protocol - creating a client

【1】Call the Dial function: (under the net package)

【2】Code:

 package main import ( "fmt" //所需的网络编程全部都在net包下 "net" ) func main() { fmt.Println("客户端启动...") //调用Dial函数:参数需要指定tcp协议,需要指定服务器端的IP+PORT conn,err := net.Dial("tcp","101.201.48.167:80") if err != nil { fmt.Println("客户端连接失败:err:",err) return } fmt.Println("连接成功,conn:",conn) }

Network communication based on TCP protocol - creating a server

【1】Monitoring: (Listen function is in the net package)

【2】Code:

 package main import ( "fmt" "net" ) func main() { fmt.Println("服务端启动了...") listen,err := net.Listen("tcp","127.0.0.1:8888") if err != nil { fmt.Println("监听失败,err:",err) return } for{ conn,err2 := listen.Accept() if err2 != nil { fmt.Println("客户端的等待失败,err2:",err2) }else { fmt.Printf("等待连接成功,con=%v,接收到的客户端信息:%v \n",conn,conn.RemoteAddr().String()) } } }

Network communication based on TCP protocol - processing terminal data

【1】The client sends data:

 package main import ( "fmt" //所需的网络编程全部都在net包下 "net" "bufio" "os" ) func main() { fmt.Println("客户端启动...") //调用Dial函数:参数需要指定tcp协议,需要指定服务器端的IP+PORT conn,err := net.Dial("tcp","127.0.0.1:8888") if err != nil { fmt.Println("客户端连接失败:err:",err) return } fmt.Println("连接成功,conn:",conn) //通过客户端发送单行数据,然后退出: reader := bufio.NewReader(os.Stdin) //从终端读取一行用户输入的信息: str,err := reader.ReadString('\n') if err != nil { fmt.Println("终端输入失败,err",err) } //将str数据发送给服务器: n,err := conn.Write([]byte(str)) if err != nil { fmt.Println("连接失败,err:",err) } fmt.Printf("终端数据通过客户端发送成功,一共发送了%d字节的数据,并退出",n) }

【2】The server receives data:

 package main import ( "fmt" "net" ) func process(conn net.Conn) { //连接用完一定要关闭: defer conn.Close() for { //创建一个切片,准备:将读取的数据放入切片 buf := make([]byte, 1024) //从conn连接中读取数据: n,err := conn.Read(buf) if err != nil { return } //将读取内容在服务器端输出: fmt.Println(string(buf[0:n])) } } func main() { fmt.Println("服务端启动了...") listen,err := net.Listen("tcp","127.0.0.1:8888") if err != nil { fmt.Println("监听失败,err:",err) return } for{ conn,err2 := listen.Accept() if err2 != nil { fmt.Println("客户端的等待失败,err2:",err2) }else { fmt.Printf("等待连接成功,con=%v,接收到的客户端信息:%v \n",conn,conn.RemoteAddr().String()) } //准备一个协程,协程处理客户端服务请求: //不同的客户端的请求,连接conn不一样的 go process(conn) } }

<<:  The difference and application of single-mode fiber and multi-mode fiber

>>:  How intermittent-link ribbon fiber revolutionizes the communications industry

Recommend

Network equipment and protocols: protocols

When visiting a website and sending or receiving ...

VirMach: $1.15/month KVM-512MB/15GB/1TB/multiple data centers available

VirMach released several special packages on Blac...

2017Q1 China Wireless Router Market Research Report

With the popularity of WiFi and mobile devices, w...

Several thinking patterns that need to be changed in the 6G era

First of all, 5/6G is born for the interconnectio...

RackNerd: $11.88/year KVM-1.2GB/25GB/2.5TB/Los Angeles data center

RackNerd announced the hard disk upgrade of DC02 ...

Network knowledge: Detailed explanation of DNS access principle

Today I will introduce the DNS access principle t...

The Current State and Future of IoT Connectivity

In every sense, the Internet of Things is about c...

Integrating SD-WAN and UCaaS has both benefits and challenges

Today, communications come in many forms, includi...

Wenku: 5G terminal connections have exceeded 200 million

On December 24, 2020, at a press conference held ...