Created
April 21, 2020 11:37
-
-
Save lisqorz/165204c1703b3cad325231ecbade3876 to your computer and use it in GitHub Desktop.
65535测试-客户端与服务端
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"sync" | |
"time" | |
) | |
func main() { | |
wait := sync.WaitGroup{} | |
incr := 0 | |
for i := 0; i < 28233; i++ { | |
wait.Add(1) | |
go func() { | |
retry := 3 | |
a: | |
conn, err := net.Dial("tcp", "192.168.89.129:8888") | |
if err != nil { | |
fmt.Println(incr, "err retry times", retry) | |
if retry == 0 { | |
fmt.Println(incr,"err retry fail",err.Error()) | |
return | |
} | |
retry-- | |
time.Sleep(2000) | |
goto a | |
} | |
incr++ | |
wait.Done() | |
fmt.Printf("current:%d\n", incr) | |
for true { | |
_, err := conn.Write([]byte("PING")) | |
if err != nil { | |
return | |
} | |
time.Sleep(3 * time.Second) | |
} | |
fmt.Printf("timeout") | |
incr-- | |
fmt.Println("current:", incr) | |
}() | |
} | |
wait.Wait() | |
fmt.Println("finish") | |
select {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"sync" | |
"time" | |
) | |
func main() { | |
serv, err := net.Listen("tcp", "192.168.89.129:8888") | |
incr := 0 | |
if err != nil { | |
panic(fmt.Sprintf("listen error:%s", err.Error())) | |
} | |
fmt.Println("listening...") | |
wait := sync.WaitGroup{} | |
for true { | |
fmt.Println("accepting...") | |
client, err := serv.Accept() | |
fmt.Println("new client") | |
if err != nil { | |
panic(fmt.Sprintf("accept error:%s", err.Error())) | |
} | |
wait.Add(1) | |
go func() { | |
defer wait.Done() | |
fmt.Println("localAddr:%s", client.LocalAddr().String()) | |
fmt.Println("remoteAddr:%s", client.RemoteAddr().String()) | |
incr++ | |
fmt.Println("current:", incr) | |
go func() { | |
var buf []byte | |
for true { | |
client.Read(buf) | |
time.Sleep(3*time.Second) | |
} | |
}() | |
}() | |
} | |
wait.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment