英文:
No connection could be made because the target machine actively refused it Golang
问题
我有一个服务器:
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() {
// 一些代码
}()
}
}
还有一个客户端,试图连接到我的计算机:
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.
为什么?我的防火墙已关闭。
英文:
I have server:
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
}
}()
}
}
and client, which trying to connect to my computer:
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))
}
My system is Kubuntu 20.04, I compiling client for windows, and send it to my friend. On friend machine, he launches it, and receives error:
Error during Dialing
dial tcp 37.57.79.119:12345: connectex: No connection could be made because the target machine actively refused it.
Why? My Firewall is off.
答案1
得分: 4
net.Listen("tcp", "localhost:12345")
正在监听localhost
,客户端正在尝试通过IP连接。
尝试在localhost
上连接客户端
net.Dial("tcp", "localhost:12345")
或者在0.0.0.0
上监听服务器
net.Listen("tcp", "0.0.0.0:12345")
英文:
net.Listen("tcp", "localhost:12345")
is listening on localhost
and client is trying to connect by IP.
Try connecting client on localhost
net.Dial("tcp", "localhost:12345")
Or listen the server on 0.0.0.0
net.Listen("tcp", "0.0.0.0:12345")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论