英文:
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:10000
或88.39.116.204:56210
。
在你的例子中有两个连接:src
和dst
(顺便说一下,你给它们起错了名字: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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论