我的服务器:
func main() {
ln, err := net.Listen("tcp", "localhost:12345")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println("Error during Accept")
fmt.Println(err)
return
}
_, err = ws.Upgrade(conn)
if err != nil {
fmt.Println("Error during Upgrade")
fmt.Println(err)
return
}
go func() {
some code
}
}()
}
}
和尝试连接到我的计算机的客户端:
func main() {
buf := new(bytes.Buffer)
payload := []byte("Hello World!")
err := binary.Write(buf, binary.LittleEndian, payload)
if err != nil {
fmt.Println("Error during writing into Binary")
fmt.Println(err)
return
}
conn, err := net.Dial("tcp", "37.57.79.119:12345")
if err != nil {
fmt.Println("Error during Dialing")
fmt.Println(err)
return
}
buf.WriteTo(conn)
defer conn.Close()
answer, _ := io.ReadAll(conn)
fmt.Println(string(answer))
}
我的系统是Kubuntu 20.04,我编译客户端为windows,并把它发送给我的朋友。在朋友的机器上,他启动它,并收到错误:
Error during Dialing
dial tcp 37.57.79.119:12345: connectex: No connection could be made because the target machine actively refused it.
为什么?我的防火墙关闭了。
1条答案
按热度按时间62lalag41#
net.Listen("tcp", "localhost:12345")
正在侦听localhost
,客户端正在尝试通过IP连接。尝试连接
localhost
上的客户端或者侦听
0.0.0.0
上的服务器