han-joker 2 years ago
commit 2f6c137724

42
.gitignore vendored

@ -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,3 @@
module netProgram
go 1.20

@ -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,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)
}
}

@ -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…
Cancel
Save