c# - 套接字异常 : ErrorCode vs. 套接字错误代码

标签 c# sockets error-handling

ErrorCode 和 SocketErrorCode 是否总是相同的值但表示为不同的类型?

SocketException class description给出了大部分相同的描述,但我没有看到任何明确说明它们具有相同的值。

SocketErrorCode 似乎更有用,因为它是一个枚举,您可以编写更漂亮的代码,例如:

if(se.SocketErrorCode == SocketError.Interrupted)

而不是:

if (se.ErrorCode != 10004)

if (se.ErrorCode != (int)SocketError.Interrupted)

最佳答案

是的,它们完全一样。

查看the source code :

public override int ErrorCode {
    //
    // the base class returns the HResult with this property
    // we need the Win32 Error Code, hence the override.
    //
    get {
        return NativeErrorCode;
    }
}

public SocketError SocketErrorCode {
    //
    // the base class returns the HResult with this property
    // we need the Win32 Error Code, hence the override.
    //
    get {
        return (SocketError)NativeErrorCode;
    }
}

关于c# - 套接字异常 : ErrorCode vs. 套接字错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24372866/

相关文章:

c# - 从 JToken 中获取可能不存在的值(value)(最佳实践)

c# - Jquery Accordion : How do I get rid of these scroll bars for good?

html - Flash 中的网络套接字

javascript - ASP 处理程序未向托管环境中的 ajax 调用返回错误消息

c# - AngularJs 脚本在部署后不起作用,它给出错误 [$injector :unpr]

c# - 无法(或能够)List<int>.Cast<Enum>()?

qt - QUdpSocket和QThread

c# - 2 TcpClient 在同一台机器和端口上

asynchronous - 具有异步操作的面向铁路的编程

python - : TypeError: not all arguments converted during string formatting的原因和建议