commit
2f6c137724
@ -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/
|
@ -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()
|
||||
}
|
@ -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()
|
||||
}
|
Loading…
Reference in new issue