Martin
Martin

Reputation: 2096

Converting SocketError value to a string message

Having a SocketError value, how can it be converted to the respective string message?

Background and Example:

A failing call to e.g. Socket.Receive will throw a SocketException with e.g. Message “An existing connection was forcibly closed by the remote host” and SocketErrorCode == SocketError.ConnectionReset. However, calling Socket.ReceiveAsync will only give a SocketError == SocketError.ConnectionReset.

Having this SocketError.ConnectionReset (or any other value), how can I get the corresponding message?

Upvotes: 2

Views: 1050

Answers (1)

Martin
Martin

Reputation: 2096

It can be done

  • using the FormatMessage API function (casting the SocketError to an int),
  • or simpler, by

    new Win32Exception((int) mySocketError).Message
    

Upvotes: 2

Related Questions