当前位置:网站首页>golang7_ TCP programming
golang7_ TCP programming
2022-06-29 23:55:00 【A teddy bear】
Basic introduction of network programming
There are two kinds of network programming :
1)TCP socket Programming , Is the mainstream of network programming . The name Tcp socket Programming , Because the bottom layer is based on Tcp/ip Association Arguable . such as : QQ Chat
2)b/s Structural http Programming , When we use the browser to access the server , What you use is http agreement , and http Bottom layer Old is used tcp socket Realized . such as : Jingdong Mall
tcp socket Programming
Write a server program , stay 8888 Port listening You can create links with multiple clients
After the link is successful , The client can send data , Server side accepts data , And displayed on the terminal .
First use telnet To test , Then write a client program to test
server
func process(conn net.Conn) {
// Here we receive the data sent by the client circularly
defer conn.Close() // close conn
for {
// Create a new slice
buf := make([]byte, 1024)
//conn.Read(buf)
//1. Wait for the client to pass conn Send a message
//2. If the client does not wrtie[ send out ], So the coroutine is stuck here
//fmt.Printf(" The server is waiting for the client %s Send a message \n", conn.RemoteAddr().String())
n , err := conn.Read(buf) // from conn Read
if err != nil {
fmt.Printf(" Client exit err=%v", err)
return //!!!
}
//3. Display the content sent by the client to the terminal of the server
fmt.Print(string(buf[:n]))
}
}
func main() {
fmt.Println(" The server starts listening ....")
//net.Listen("tcp", "0.0.0.0:8888")
//1. tcp Indicates that the use of network protocol is tcp
//2. 0.0.0.0:8888 Means listening locally 8888 port
listen, err := net.Listen("tcp", "0.0.0.0:8888")
if err != nil {
fmt.Println("listen err=", err)
return
}
defer listen.Close() // Delay off listen
// Loop waiting for the client to link me
for {
// Waiting for client link
fmt.Println(" Wait for the client to link ....")
conn, err := listen.Accept()
if err != nil {
fmt.Println("Accept() err=", err)
} else {
fmt.Printf("Accept() suc con=%v client ip=%v\n", conn, conn.RemoteAddr().String())
}
// Here is a collaborative process , Serving clients
go process(conn)
}
//fmt.Printf("listen suc=%v\n", listen)
}
client
func main() {
conn, err := net.Dial("tcp", "192.168.20.253:8888")
if err != nil {
fmt.Println("client dial err=", err)
return
}
// Function one : The client can send a single line of data , And then quit
reader := bufio.NewReader(os.Stdin) //os.Stdin For standard input [ terminal ]
for {
// Read a line of user input from the terminal , And ready to send it to the server
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println("readString err=", err)
}
// If the user enters exit Quit
line = strings.Trim(line, " \r\n")
if line == "exit" {
fmt.Println(" Client exit ..")
break
}
// then line Send to The server
_, err = conn.Write([]byte(line + "\n"))
if err != nil {
fmt.Println("conn.Write err=", err)
}
}
}
边栏推荐
猜你喜欢

Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree

剑指 Offer 13. 机器人的运动范围

雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前

Collection! Have you ever used these tools to improve programmer productivity?

简单理解B树和B+树

數莓派 4怎麼樣?可能的玩法有哪些?

Under the epidemic, I left my job for a year, and my income increased 10 times

FPGA Development (1) -- serial port communication

Matlab exercises -- program control process exercise

Software testing interface testing postman testing tool interface testing process execution interface testing interface associated environment variables and global variables built-in dynamic parameter
随机推荐
搭建企业级NTP时间服务器
Solr基础操作4
I wonder if I can open an account today? In addition, is it safe to open an account online now?
shell-位置参数变量和预定义变量
Fund valuation, expenses and accounting
AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
请指教什么是在线开户?另外,手机开户安全么?
西门子低代码 9.14版本: 满足不同需求
Virtual machine online migration based on openstack
Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
剑指 Offer 14- I. 剪绳子
Solr basic operation 4
Collection! Have you ever used these tools to improve programmer productivity?
【一起上水硕系列】Day 8
招商证券靠谱吗?开股票账户安全吗?
为什么 JSX 语法这么香?
Halcon实用:焊点检出设计思路
modelsim的TCL脚本的define incdir命令解析
云服务器的安全设置常识
Solr基础操作2