滤除破损的管道错误 [英] Filter out broken pipe errors

查看:268
本文介绍了滤除破损的管道错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误,它是从 io.Copy 调用返回的,我通过了套接字( TCPConn )作为目标。预计远程主机会在他们有足够的连接时断开连接,并且我没有收到任何连接。

I'm getting an error returned from an io.Copy call, to which I've passed a socket (TCPConn) as the destination. It's expected that the remote host will simply drop the connection when they've had enough, and I'm not receiving anything from them.

当发生丢包时,我得到这个错误:

When the drop occurs, I get this error:

write tcp 192.168.26.5:21277: broken pipe

但我所拥有的是一个错误界面。

But all I have is an error interface. How can I differentiate broken pipe errors from other kinds of error?

if err.Errno == EPIPE...


推荐答案

破损管道错误在syscall包中定义。您可以使用相等运算符将错误与系统调用中的错误进行比较。查看 http://golang.org/pkg/syscall/#constants 获取系统调用错误的完整列表。搜索页面上的EPIPE,你会发现所有已定义的错误组合在一起。

The broken pipe error is defined in the syscall package. You can use the equality operator to compare the error to the one in syscall. Check http://golang.org/pkg/syscall/#constants for a complete list of syscall errors. Search "EPIPE" on the page and you will find all the defined errors grouped together.

if err == syscall.EPIPE {
    /* ignore */
}

如果你想获得真正的errno数字(尽管它非常没用),你可以使用类型断言: p>

If you wish to get the actual errno number (although it is pretty useless) you can use a type assertion:

if e, ok := err.(syscall.Errno); ok {
    errno = uintptr(e)
}

这篇关于滤除破损的管道错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆