5

I am writing an app which listens tcp connection (see this example) . When a tcp connection disconnected I got error read tcp ip_server.:port1->ip_client:port2: wsarecv: An existing connection was forcibly closed by the remote host. I expected error EOF and timeout(for conn.SetReadDeadline()) and tried to catch error with this code:

if err != nil {     
    log.Println("getting error from listener")
    // I thought, listener can continue work another cases
    if neterr, ok := err.(net.Error); ok && neterr.Timeout() || err == io.EOF {
        log.Println("Closing connection...")
        break // connection will be closed 
    }
}

Anyone knows about this error? Do you know how to catch this error and when this error will occured ? Thanks in advance !

Ulug'bek
  • 2,762
  • 6
  • 31
  • 59
  • 1
    Not with Go but the same kind of error : https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host – HectorJ Sep 03 '15 at 11:04

1 Answers1

7

The usual cause of this error is that you sent data over a connection which had already been closed by the peer. In other words, an application protocol error.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    @Ulug'bekRo'zimboyev, two easy ways to find out: a) look at the Go `net` package source or b) use `log.Printf("%T %+v", err, err)` to format/print both the full type and full information contained in `err` when it occurs, that will tell you what code you need to replace the `Printf` with. – Dave C Sep 03 '15 at 17:18
  • 1
    Did you find a type for this message? – tmm1 Jan 27 '17 at 21:21
  • I get `*net.OpError read tcp ip_server.:port1->ip_client:port2: wsarecv: An existing connection was forcibly closed by the remote host.`, so the type of the message should be `*net.OpError`. – levelont Jun 21 '18 at 13:54
  • @Ulug'bekRo'zimboyev Ultimately it is ECONNRESET. What that maps to in *go* is unknown to me, but you might look it up. – user207421 Feb 28 '19 at 10:33
  • This happens to me sometimes when I forward port of Argo server by ```kubectl -n argo-events port-forward deployment/argo-server 2746:2746``` How to fix it? – Eduard Nickel Nov 05 '21 at 09:37