From 2f6c137724a6f67bd141bf9a7ca4a150de1316aa Mon Sep 17 00:00:00 2001 From: han-joker Date: Fri, 28 Apr 2023 19:17:54 +0800 Subject: [PATCH] tcp 1 --- .gitignore | 42 +++++++++++++++++++++ go.mod | 3 ++ tcp_client.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ tcp_server.go | 76 +++++++++++++++++++++++++++++++++++++ tcp_test.go | 23 ++++++++++++ 5 files changed, 245 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 tcp_client.go create mode 100644 tcp_server.go create mode 100644 tcp_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9cd587c --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +# Reference https://github.com/github/gitignore/blob/master/Go.gitignore +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +vendor/ + +# Go workspace file +go.work + +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# OS General +Thumbs.db +.DS_Store + +# project +*.cert +*.key +*.log +bin/ + +# Develop tools +.vscode/ +.idea/ +*.swp + +# volumes +data/ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..63ac3d4 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module netProgram + +go 1.20 diff --git a/tcp_client.go b/tcp_client.go new file mode 100644 index 0000000..79afc44 --- /dev/null +++ b/tcp_client.go @@ -0,0 +1,101 @@ +package netProgram + +import ( + "log" + "net" + "sync" + "time" +) + +// 客户端 +func TcpClient() { + // tcp服务端地址 + //serverAddress := "127.0.0.1:5678" + + // tcp协议类型 + serverAddress := "[::1]:5678" // IPv6 + //serverAddress := "127.0.0.1:5678" // IPv4 + + // 模拟多客户端 + // 并发的客户端请求 + num := 10 + wg := sync.WaitGroup{} + wg.Add(num) + for i := 0; i < num; i++ { + // 并发请求 + go func(wg *sync.WaitGroup) { + defer wg.Done() + // A. 建立连接 + conn, err := net.Dial(tcp, serverAddress) + if err != nil { + log.Println(err) + return + } + // 保证关闭 + defer conn.Close() + log.Printf("connection is establish, client addr is %s\n", conn.LocalAddr()) + }(&wg) + } + + wg.Wait() +} + +func TcpTimeoutClient() { + // tcp服务端地址 + serverAddress := "192.168.110.123:5678" // IPv6 4 + + // 模拟多客户端 + // 并发的客户端请求 + num := 10 + wg := sync.WaitGroup{} + wg.Add(num) + for i := 0; i < num; i++ { + // 并发请求 + go func(wg *sync.WaitGroup) { + defer wg.Done() + // A. 建立连接 + conn, err := net.DialTimeout(tcp, serverAddress, time.Second) + //conn, err := net.Dial(tcp, serverAddress) + if err != nil { + log.Println(err) + return + } + // 保证关闭 + defer conn.Close() + log.Printf("connection is establish, client addr is %s\n", conn.LocalAddr()) + }(&wg) + } + + wg.Wait() +} + +func TcpBacklogClient() { + // tcp服务端地址 + serverAddress := "127.0.0.1:5678" // IPv6 4 + + // 模拟多客户端 + // 并发的客户端请求 + num := 256 + wg := sync.WaitGroup{} + wg.Add(num) + for i := 0; i < num; i++ { + // 并发请求 + go func(wg *sync.WaitGroup, no int) { + defer wg.Done() + // A. 建立连接 + conn, err := net.DialTimeout(tcp, serverAddress, time.Second) + //conn, err := net.Dial(tcp, serverAddress) + if err != nil { + log.Println(err) + return + } + // 保证关闭 + defer conn.Close() + log.Printf("%d: connection is establish, client addr is %s\n", no, conn.LocalAddr()) + }(&wg, i) + + time.Sleep(30 * time.Millisecond) + } + + wg.Wait() +} diff --git a/tcp_server.go b/tcp_server.go new file mode 100644 index 0000000..ab0fb6f --- /dev/null +++ b/tcp_server.go @@ -0,0 +1,76 @@ +package netProgram + +import ( + "log" + "net" + "time" +) + +const tcp = "tcp" + +// 服务端 +func TcpServer() { + // A. 基于某个地址建立监听 + // 服务端地址 + //address := "127.0.0.1:5678" + // 端口省略,会使用随机端口 + //address := "127.0.0.1:" + // IP省略,使用服务器全部的可用IP + + // tcp协议类型 + //address := "127.0.0.1:5678" // IPv4 + //address := "[::1]:5678" // IPv6 + address := ":5678" // Any IP or version + listener, err := net.Listen(tcp, address) + if err != nil { + log.Fatalln(err) + } + // 关闭监听 + defer listener.Close() + log.Printf("%s server is listening on %s\n", tcp, listener.Addr()) + + // B. 接受连接请求 + // 循环接受 + for { + // 阻塞接受 + conn, err := listener.Accept() + if err != nil { + log.Println(err) + } + + // 处理连接,读写 + // 日志连接的远程地址(client addr) + log.Printf("accept from %s\n", conn.RemoteAddr()) + } +} + +// 服务端 +func TcpBacklogServer() { + // A. 基于某个地址建立监听 + // 服务端地址 + address := ":5678" // Any IP or version + listener, err := net.Listen(tcp, address) + if err != nil { + log.Fatalln(err) + } + // 关闭监听 + defer listener.Close() + log.Printf("%s server is listening on %s\n", tcp, listener.Addr()) + + // B. 接受连接请求 + // 循环接受 + for { + // 阻塞接受 + conn, err := listener.Accept() + if err != nil { + log.Println(err) + } + + // 处理连接,读写 + go func(conn net.Conn) { + // 日志连接的远程地址(client addr) + log.Printf("accept from %s\n", conn.RemoteAddr()) + time.Sleep(time.Second) + }(conn) + } +} diff --git a/tcp_test.go b/tcp_test.go new file mode 100644 index 0000000..9a185ea --- /dev/null +++ b/tcp_test.go @@ -0,0 +1,23 @@ +package netProgram + +import "testing" + +func TestTcpServer(t *testing.T) { + TcpServer() +} + +func TestTcpClient(t *testing.T) { + TcpClient() +} + +func TestTcpTimeoutClient(t *testing.T) { + TcpTimeoutClient() +} + +func TestTcpBacklogServer(t *testing.T) { + TcpBacklogServer() +} + +func TestTcpBacklogClient(t *testing.T) { + TcpBacklogClient() +}