如何确定在“使用关闭的网络连接”错误中哪个连接已关闭

huangapple go评论98阅读模式
英文:

How to know which connection closed in "use of closed network connection" error

问题

我正在使用io.Copy在Go中代理TCP连接:

_, err := io.Copy(src, dst)
if err != nil {
   log.Println(err)
}

当一个连接关闭时,会发送以下错误信息:

readfrom tcp 171.31.80.49:10000->88.39.116.204:56210: use of closed network connection

我如何知道哪个网络连接关闭了?即171.31.80.49:10000还是88.39.116.204:56210

英文:

I'm proxying TCP connections in Go using io.Copy

_, err := io.Copy(src, dst)
if err != nil {
   log.Println(err)
}

and one connection closes therefore sending this error:

readfrom tcp 171.31.80.49:10000->88.39.116.204:56210: use of closed network connection

How do I know which network connection closed? i.e. 171.31.80.49:10000 or 88.39.116.204:56210.

答案1

得分: 6

一个TCP连接是一对IP和端口对。在你的情况下,连接是171.31.80.49:10000->88.39.116.204:56210。它是连接,并且已关闭。没有连接171.31.80.49:1000088.39.116.204:56210

在你的例子中有两个连接:srcdst(顺便说一下,你给它们起错了名字:https://pkg.go.dev/io#Copy)。如果你的问题是哪个连接被关闭了,那么根据错误消息,应该是dst(本应该命名为src)。

为什么?因为消息说:readfrom ...,错误发生在io.Copy从Reader读取时,而在我们的情况下,Reader是dst

英文:

A TCP connection is a pair of IP and port pairs. In your case, the connection is 171.31.80.49:10000->88.39.116.204:56210. It is the connection, and it is closed. There is no connection 171.31.80.49:10000 or 88.39.116.204:56210.

There are two connections in your example: src and dst (you misnamed them, by the way: https://pkg.go.dev/io#Copy). If your question is which connection is getting closed, then, according to the error message, it's dst (which is supposed to be named src).

Why? Because the message says: readfrom ..., the error occurs when the io.Copy is reading from the Reader, which in our case is dst.

huangapple
  • 本文由 发表于 2021年12月31日 06:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/70537254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定