> I assume you mean System.Net.Sockets.Socket here. But for inspiration,
> there is already something in the framework which does what you
> consider: System.Net.Sockets.TcpClient is what Peter suggests, a class
> that implements the application protocol (in a way) and contains a
> Socket.
On Aug 15, 10:23 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
> I don't agree that TcpClient implements an application protocol. I
> realize you wrote "in a way", [...]
But you couldn't leave it at that, could you. No, you had to go all
OSI on me! I was trying not to confuse the original poster, but now
you've unleashed hell. =]
> And even TcpClient doesn't inherit Socket, even though it is about as
> close as you can get to a class that's a Socket without being a Socket. :)
I like to think of TcpClient as a managed socket, and Socket as a very
thin Winsock2 wrapper. This tends to lead to very few surprises. The
error handling is IMHO the thing about Socket that's the least
managed, since the general approach is to wrap the Winsock2 error in a
SocketException. In an analogy: if the File class just threw a
FileException with ERROR_INVALID_TARGET_HANDLE in it instead of
throwing a specialized Exception, I think people would be
disappointed.
> There _are_ application-protocol-specific classes, and they also don't
> inherit the Socket class. See the WebRequest class and its inheritors
> for examples of that.
Bet you didn't know that HttpWebRequest is one of the largest classes
in the framework!
> I guess what I'm getting at is that I'm not convinced that inheriting
> the Socket class is a good design.
I agree with what you wrote above: only inherit from Socket if you're
writing a specialized type of Socket. I tend to use extension methods
to make Sockets smarter. Also, I think the Socket class is a bit too
unpredictable to serve as a good base class. Look at the Dispose()
method (which is the only virtual or protected member), for example. I
wonder if it isn't a good candidate for sealing...
Peter Duniho - 15 Aug 2007 18:13 GMT
> But you couldn't leave it at that, could you. No, you had to go all
> OSI on me!
"OSI"?
> I was trying not to confuse the original poster, but now
> you've unleashed hell. =]
Um. Hell? Okay. If you say so.
> I like to think of TcpClient as a managed socket, and Socket as a very
> thin Winsock2 wrapper.
I guess that's fair enough. However, I still find Socket to have some
important characteristics of a managed class. Specifically, it wraps
the use of i/o completion ports in a very simple, easy-to-use way.
You are right about the error handling being different, but at least
there's the SocketError enumeration. That is, in other "unmanaged"
areas of .NET, you pretty much just have to copy and paste values from
the unmanaged Windows header files. For example, various window message
codes that you might use in WndProc.
And of course, TcpClient doesn't completely hide you from the
error-handling of the Socket class anyway. It can still throw a
SocketException, which just takes you right back to the Socket class.
> This tends to lead to very few surprises. The
> error handling is IMHO the thing about Socket that's the least
[quoted text clipped - 3 lines]
> throwing a specialized Exception, I think people would be
> disappointed.
Maybe. Though, I note there's about twice as many different errors a
Socket can throw via SocketException than there are various file
i/o-related exceptions. That'd be a lot of exceptions defined in the
namespace.
Also, in the case of the Socket class where the handling for specific
errors may be different, but is in fact shared between some specific
errors, it's simpler to write a single exception clause that inspects
the SocketError value, rather than creating multiple clauses that all
just call a common error-handling method.
I can't say that I've found myself disappointed with the design of the
Socket class, error-handling aspects or otherwise. I find it to be a
very nice enhancement to the underlying Winsock API.
Pete
UL-Tomten - 15 Aug 2007 18:55 GMT
> "OSI"?
Open Systems Interconnection Basic Reference Model. It defines 7
network layers, where IP is below TCP is below HTTP, etc. I envy your
buzzword-free existence.
> I can't say that I've found myself disappointed with the design of the
> Socket class, error-handling aspects or otherwise. I find it to be a
> very nice enhancement to the underlying Winsock API.
I agree, which is the very reason I don't think the Socket class is
anywhere near as mature as other classes. Specifically, I think the
managed socket API has done too little to overcome the problems
connected to leaky abstractions[1]. For me, exposing all the
SocketErrors is a good thing, because I'm somewhat used to them. But
for the beginner?
1: http://www.joelonsoftware.com/articles/LeakyAbstractions.html