想把流量发个多个后端,每个后端都可以收到内容,但我写的,每次都只有一个后端收到内容,随机的
```
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)
}
}
```
该如何改呢,哪位大佬帮忙看看
![code.png](https://static.golangjob.cn/220923/fad00eee64aaea2a05b0a107d29b4508.png)
#9
更多评论
某个协程io.Copy之后相当于读完了流,另外的协程当然读不到数据了,你需要修改代码,每次读取[]byte,然后再每个连接Write([]byte)
#1