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

[Black Friday] TNAHosting: $12/year KVM-1GB/15G SSD/5TB/Chicago

TNAHosting is a relatively early established fore...

[Security Alert] Baota Panel suspected vulnerability or Nginx abnormality

For the convenience of many friends, panels are d...

Five common OSPF problems

I am Man Guodong, a lecturer at 51CTO Academy. On...

What exactly is Wi-Fi 6?

Wi-Fi has been around for more than two decades, ...

Network security knowledge: View the user license agreement

It is very necessary to understand the user licen...

PacificRack: $6.66/year KVM-512MB/15GB/1TB/Los Angeles

PacificRack's 2021 New Year flash sale has al...

Don’t always blame your router for a bad network. The truth is here.

It is undeniable that with the development of the...

6G in 2030: The panacea for enterprises is here again

While 5G is still positioned as a “near-term” gam...