TCP的C/S架构
TCP服务器的实现
监听的底层实现
func Listen(network, address string) (Listener, error) {
var lc ListenConfig
return lc.Listen(context.Background(), network, address)
}
type Listener interface {
// Accept waits for and returns the next connection to the listener.
Accept() (Conn, error)
// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
Close() error
// Addr returns the listener's network address.
Addr() Addr
}
- 服务端阻塞等待客户端的连接
- 可以使用netcat工具对其进行测试
netcat下载地址netcat下载地址
TCP客户端的实现
package main
import (
"fmt"
"net"
)
func main() {
//主动连接服务器
conn, err := net.Dial("tcp", "127.0.0.1:8000")
if err != nil {
fmt.Println("err=", err)
return
}
defer conn.Close()
conn.Write([]byte("you ok?"))
}
标签:listener,return,Addr,err,TCP,---,Go,conn
From: https://blog.csdn.net/m0_73537205/article/details/140129858