voidmnwzp
2023-07-19 20:01:08 +08:00
网页端的正文按 ctrl+enter 居然是直接发送无法换行 什么 nt 设定。。。。
``` go
host0 := "remote"
port0 := "8080"
host1 := "remote"
port1 := "689"
// 本机端口
localPort := "12301"
// 创建本机地址
localAddr, err := net.ResolveTCPAddr("tcp", "localhost:"+localPort)
if err != nil {
fmt.Println("Error resolving local address:", err)
os.Exit(1)
}
// 创建远程地址 8080
remoteAddr0, err := net.ResolveTCPAddr("tcp", host0+":"+port0)
if err != nil {
fmt.Println("Error resolving remote address:", err)
os.Exit(1)
}
// 创建远程地址 689
remoteAddr1, err := net.ResolveTCPAddr("tcp", host1+":"+port1)
if err != nil {
fmt.Println("Error resolving remote address:", err)
os.Exit(1)
}
// 建立连接
go dial(localAddr, remoteAddr0)
dial(localAddr, remoteAddr1)
```
``` go
func dial(l, r *net.TCPAddr) {
localConn, err := net.DialTCP("tcp", l, r)
if err != nil {
fmt.Println("Error connecting to remote host:", err)
os.Exit(1)
}
localConn.Write([]byte("ping"))
}
```
bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
我想用本机上的同一个端口去连接不同的服务器,但这个好像在 netty 上可以实现,不知道在 go 上如何实现,go 在 dialTcp 的时候,将 socket bind 了端口