Home > other >  No connection could be made because the target machine actively refused it Golang
No connection could be made because the target machine actively refused it Golang

Time:08-13

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.

CodePudding user response:

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