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 !

1

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@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.
Did you find a type for this message?
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.
@Ulug'bekRo'zimboyev Ultimately it is ECONNRESET. What that maps to in go is unknown to me, but you might look it up.
@EduardNickel Without a proper question context provided by you it is impossible to say, but have you considered the possibility that it is still an application protocol error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.