当前位置:网站首页>Golang -- TCP implements concurrency (server and client)
Golang -- TCP implements concurrency (server and client)
2022-07-06 04:55:00 【Java mage】
Server End common function 、 Interface :
Listen function :
func Listen(network, address string) (Listener, error)
network: Optional Protocol :TCP、UDP, Such as :“tcp” or “udp”
address:IP Address + Port number , Such as :“127.0.0.1:8000” or “:8000”
Listener Interface :
type Listener interface {
Accept() (Conn, error)
Close() error
Addr() Addr
}
Conn Interface :
type Conn interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
Close() error
LocalAddr() Addr
RemoteAddr() Addr
SetDeadline(t time.Time) error
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
}
TCP- Server implementation :
func main() {
// Specify the communication protocol of the server 、ip、 port ,Listen Do not monitor by itself , This step is to create a for listening Socket
listen, err := net.Listen("tcp", "127.0.0.1:8000")
if err != nil {
fmt.Println("net.Listen error :", err)
return
}
defer listen.Close()
fmt.Println(" The server is up , Wait for the client to connect ")
// Block listening for client connection requests , After the connection is successfully established, the... For communication will be returned Socket
accept, err := listen.Accept()
if err != nil {
fmt.Println("listen.Accept error :", err)
return
}
defer accept.Close()
fmt.Println(" The connection between the server and the client was successful ")
// Read the request initiated by the client
buf := make([]byte, 4096)
read, err := accept.Read(buf)
if err != nil {
fmt.Println("accept.Read error :", err)
return
}
// Process data after receiving data
fmt.Println(" Server get to :", string(buf[:read]))
}
Mac Can pass netcat To test :
TCP- Client implementation :
Server side :
func main() {
// Specify the communication protocol of the server 、ip、 port ,Listen Do not monitor by itself , This step is to create a for listening Socket
listen, err := net.Listen("tcp", "127.0.0.1:8000")
if err != nil {
fmt.Println("net.Listen error :", err)
return
}
defer listen.Close()
fmt.Println(" The server is up , Wait for the client to connect ")
// Block listening for client connection requests , After the connection is successfully established, the... For communication will be returned Socket
accept, err := listen.Accept()
if err != nil {
fmt.Println("listen.Accept error :", err)
return
}
defer accept.Close()
fmt.Println(" The connection between the server and the client was successful ")
// Read the request initiated by the client
buf := make([]byte, 4096)
read, err := accept.Read(buf)
if err != nil {
fmt.Println("accept.Read error :", err)
return
}
accept.Write(buf[:read]) // Read the data and write it back
// Process data after receiving data
fmt.Println(" Server get to :", string(buf[:read]))
}
client :
func main() {
// Specify the communication protocol of the client 、ip、 port ,Listen Do not monitor by itself , This step is to create a for listening Socket
dial, err := net.Dial("tcp", "127.0.0.1:8000")
if err != nil {
fmt.Println("net.Dial error :", err)
return
}
defer dial.Close()
// send data
dial.Write([]byte(" I'm the client "))
// Receive the data returned by the server
buf := make([]byte, 4096)
read, err := dial.Read(buf)
if err != nil {
fmt.Println("accept.Read error :", err)
return
}
// Process data after receiving data
fmt.Println(" The client gets :", string(buf[:read]))
}
TCP Implement concurrency - The server :
The above is the client communication of the stand-alone version , If you want to achieve concurrency , Need to use Goroutine+ Cycle to achieve
- Loop read data sent by client
- If the client forcibly closes the connection, it needs to be handled
- The client sends exit when
demonstration :
package main
import (
"fmt"
"net"
"strings"
)
func main() {
// Create a listening socket
listen, err := net.Listen("tcp", "127.0.0.1:8000")
if err != nil {
fmt.Println("net.Listen error :", err)
return
}
defer listen.Close()
// Create client connection request
fmt.Println(" Server started successfully , Wait for the client to connect !")
for {
accept, err := listen.Accept()
if err != nil {
fmt.Println("listen.Accept error :", err)
return
}
// Call the function that the server communicates with the client
go HandlerConnect(accept)
}
}
func HandlerConnect(accept net.Conn) {
defer accept.Close()
// Get the data sent by the client
// Get the network address of the connected client
addr := accept.RemoteAddr()
fmt.Println(addr, " Client connection successful !")
buf := make([]byte, 4096)
for {
read, err := accept.Read(buf)
if err != nil {
fmt.Println("accept.Read error :", err)
return
}
fmt.Println(" The server reads data :", string(buf[:read]))
// After the simulation server receives the data , Send back to the client , Small to capital
data := strings.ToUpper(string(buf[:read]))
accept.Write([]byte(data))
}
}
TCP Implement concurrency - client :
demonstration :
package main
import (
"fmt"
"net"
"os"
)
func main() {
// Send connection request actively
dial, err := net.Dial("tcp", "127.0.0.1:8000")
if err != nil {
fmt.Println("et.Dial Something went wrong ", err)
return
}
defer dial.Close()
// os.Stdin(): Get user keyboard entry ,
go func() {
str := make([]byte, 4096)
for {
read, err := os.Stdin.Read(str)
if err != nil {
fmt.Println("os.Stdin.Read Something went wrong ", err)
continue
}
// The read data is written to the server , Read as much as you write
dial.Write(str[:read])
}
}()
buf := make([]byte, 4096)
// Echo the data sent by the server , Capitalize
for {
read, err := dial.Read(buf)
// read=0 Description of the opposite end close connection , If you close the connection, there is no need to read data down here
if read == 0 {
fmt.Println(" It is detected that the server has been disconnected !")
return
}
if err != nil {
fmt.Println(" Echo the data sent by the server dial.Read Something went wrong ", err)
return
}
fmt.Println(" The client reads the echo data of the server ", string(buf[:read]))
}
}
边栏推荐
- Ue5 small knowledge points to enable the setting of lumen
- [mathematical modeling] differential equation -- sustainable development of fishing industry
- A little knowledge of CPU, disk and memory
- 也算是學習中的小總結
- SQL注入漏洞(MSSQL注入)
- SQL injection vulnerability (MSSQL injection)
- [Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
- Summary of three log knowledge points of MySQL
- Acwing week 58
- L'introduction en bourse de MSK Electronics a pris fin: 800 millions de RMB d'actifs de Henan étaient des actionnaires
猜你喜欢
[05-1, 05-02, 05-03] network protocol
Crazy God said redis notes
Postman前置脚本-全局变量和环境变量
Database - MySQL storage engine (deadlock)
A blog to achieve embedded entry
[Chongqing Guangdong education] engineering fluid mechanics reference materials of southwestjiaotonguniversity
Digital children < daily question> (Digital DP)
RT thread analysis - object container implementation and function
Programmers' position in the Internet industry | daily anecdotes
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
随机推荐
idea一键导包
内核判断i2c地址上是否挂载外设
On the solution of es8316's audio burst
2021robocom robot developer competition (Preliminary)
The video in win10 computer system does not display thumbnails
Codeforces Round #804 (Div. 2)
Hometown 20 years later (primary school exercises)
程序员在互联网行业的地位 | 每日趣闻
也算是學習中的小總結
Crazy God said redis notes
MySQL reported an error datetime (0) null
What should the project manager do if there is something wrong with team collaboration?
ISP学习(2)
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
关于es8316的音频爆破音的解决
图论的扩展
How does vs change the project type?
Acwing week 58
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
JS quick start (II)