Search code examples
gohttpcurltcp

curl: (56) Recv failure: Connection reset by peer | curl error | Golang TCP server


Getting this error message when trying to hit a curl on to a local server created using Golang :

curl: (56) Recv failure: Connection reset by peer

I am trying to write a TCP server code using Golang(code is mentioned below). Ideally when you run this code, the server should start running, which is happening successfully. But when I am trying to hit the following curl request : curl http://localhost:1729, I am getting Recv failure: Connection reset by peer error.

package main

import (
    "fmt"
    "log"
    "net"
)

func main() {
    listener, err := net.Listen("tcp", ":1729")
    if err != nil {
        log.Fatal(err)
    }
    conn, err := listener.Accept()
    // fmt.Println("Waiting for connection")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(conn)
}


Solution

  • "Connection reset by peer" will happen if the server closes the connection while there are still unread data from the client (among other causes). In this case a TCP RST is sent. This is exactly what is happening here: curl is sending a HTTP request but the server does not read it but instead immediately closes the connection.