无法建立连接,因为目标机器主动拒绝了它。Golang

huangapple go评论84阅读模式
英文:

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")

huangapple
  • 本文由 发表于 2022年8月13日 00:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/73337541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定