想把流量发个多个后端,每个后端都可以收到内容,但我写的,每次都只有一个后端收到内容,随机的
```
package main
import (
"flag"
"fmt"
"io"
"net"
"strings"
)
var tcp_conn []net.Conn
func main() {
var address *string
var dst *string
address = flag.String("listen_address", "127.0.0.1:8080", "listen address")
dst = flag.String("target_address", "172.16.5.9:18061,172.16.5.9:18060", "目标地址,多个地址逗号分割")
flag.Parse()
listener, err := net.Listen("tcp", *address)
if err != nil {
fmt.Println(err)
}
ips := strings.Split(*dst, ",")
if len(ips) == 0 {
fmt.Println("目标地址不能为空")
return
}
for _, ip := range ips {
conn, err := net.Dial("tcp", ip)
if err != nil {
fmt.Println(err)
continue
}
tcp_conn = append(tcp_conn, conn)
}
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handler(conn)
}
}
func handler(conn net.Conn) {
// defer conn.Close()
for _, dst_conn := range tcp_conn {
go io.Copy(dst_conn, conn)
go io.Copy(conn, dst_conn)
}
}
```
该如何改呢,哪位大佬帮忙看看
有疑问加站长微信联系(非本文作者)