Socket.BeginReceive errorCode parameter

A

Alexander Smirnov

Hello!

In .NET 2.0 Socket.BeginReceive has a new overloaded version:

public IAsyncResult BeginReceive (
byte[] buffer,
int offset,
int size,
SocketFlags socketFlags,
out SocketError errorCode,
AsyncCallback callback,
Object state )

My question is about errorCode parameter. What it is for? Is it for
returning error code in case of a socket error? But why then this
version of BeginReceive throws SocketException as MSDN says?

Thanks in advance.
 
K

Kevin Spencer

The SocketException class has a member called ErrorCode that is an integer.
This code can be any of about 50 different numbers, each signifying a
different kind of error, returned from the underlying socket. The
SocketError parameter is an enum. By calling the ToString() method on it,
you can get a meaningful string that is more human-readable for diagnostic
purposes.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
A

Alexander Smirnov

So the purpose of errorCode parameter is only for conveniance? I hoped
that it is alternative way to get info about function success or
failure without try{} catch{}.
 
K

Kevin Spencer

I'm not sure I would say "only." ;-)

There is quite a lot in the .Net Framework that is for the "convenience"
(i.e. "productivity") of the developer, including language enhancements such
as anonymous methods, etc. I have spent quite awhile looking up socket error
codes myself, and I can testify that having an enumeration is quite helpful!

But as for "an alternative way to get info about function success or failure
without try {} cat{}" I would have to say no. The SocketError enumeration
does include a few "success" codes, but if the socket doesn't throw an
exception, you're not likely to need them. All computer functions return
numeric values, and for many years Microsoft used them. But the .Net
programming model uses Exceptions which don't have numeric values (numbers
are highly counter-intuitive as descriptions to humans), but messages
instead. I believe that the enumeration is for the benefit (productivity) of
the developer/debugger.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top