2015-09-03 134 views
1

我正在編寫一個應用程序,它監聽tcp connection(請參閱this example)。當tcp connection斷開我有錯誤read tcp ip_server.:port1->ip_client:port2: wsarecv: An existing connection was forcibly closed by the remote host. 我預計錯誤EOF和超時(爲conn.SetReadDeadline()),並試圖抓住錯誤與此代碼:Golang TCP錯誤wsarecv

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 
    } 
} 

任何人都知道這個錯誤?你知道如何捕捉這個錯誤,當這個錯誤發生?提前致謝 !

+1

不與Go但同一種錯誤:https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host – HectorJ

回答

4

此錯誤的通常原因是您通過已由對等關閉的連接發送數據。換句話說,應用程序協議錯誤。

+0

這個錯誤有沒有代碼像'io.EOF'? –

+2

@ Ulug'bekRo'zimboyev,有兩個簡單的方法可以找到:a)查看Go'net'包源代碼或b)使用'log.Printf(「%T%+ v」,err,err)'格式化/在'err'中包含完整類型和完整信息時,它會告訴你用什麼代碼替換'Printf'。 –

+0

@DaveC,謝謝! –