473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

API Design: Is Socket.Select() seriously busted?


I don't know how I missed this piece for so long (this may also be old
news to you guys) but in this ACMQueue article, Michi Henning goes to
elaborate lengths to detail how even today designing a good API
requires a lot experience and effort by illustrating the example of
something as common place as the select() function call available in
sockets. The example focuses on the design decision surrounding the
Select() call in C#.

It comes under the heading "Bad APIs are easy" and goes on for about 4
pages about its problems.

The link to the article is here:
http://www.acmqueue.com/modules.php?...pid=488&page=1

What do the experts think?

Sep 27 '07 #1
6 6575

"Dilip" <rd*****@lycos.comwrote in message
news:11********************@19g2000hsx.googlegroup s.com...
>
I don't know how I missed this piece for so long (this may also be old
news to you guys) but in this ACMQueue article, Michi Henning goes to
elaborate lengths to detail how even today designing a good API
requires a lot experience and effort by illustrating the example of
something as common place as the select() function call available in
sockets. The example focuses on the design decision surrounding the
Select() call in C#.

It comes under the heading "Bad APIs are easy" and goes on for about 4
pages about its problems.

The link to the article is here:
http://www.acmqueue.com/modules.php?...pid=488&page=1

What do the experts think?
Socket.Select isn't meant to be a good, modern API, it is meant to emulate
Berkely Sockets' select() function. It looks like it does that quite well.

The suggested improvements are a big step forward from select, but pale
horribly beside event-driven programming (WSAEventSelect or WSAAsyncSelect
or a completion port which in .NET means asynchronous BeginXYZ calls with a
WaitHandle or a completion callback delegate).

Overall, Socket.Select isn't meant to be the .NET way, it's meant to ease
porting code that already uses BSD select().
Sep 27 '07 #2
Dilip, the article does not focus on just the unfortunate design of the
select() function. It uses it as an example. The part of the article I liked
most, is where the author condemns the API forcing the developer using error
trapping for intercepting error that should not be anticipated. In .NET
Socket class the async callback function will throw an error whenever the
connection is lost. This was discussed in this NG quite a few times and the
'experts' here see nothing wrong with relying on error trapping mechanism.
The author of the article calls the API design that encourages such
practices 'perverse', and I agree. There are similar snafus in .NET Serial
class. USB-Serial converters are very common and work quite well. The
problem is, if the user pulls the USB connector out while the 'mapped' com
port was open, there is no way the application can handle this situation,
even error trapping is not a solution. Very good article, thanks for posting
the link.

Michael

"Dilip" <rd*****@lycos.comwrote in message
news:11********************@19g2000hsx.googlegroup s.com...
>
I don't know how I missed this piece for so long (this may also be old
news to you guys) but in this ACMQueue article, Michi Henning goes to
elaborate lengths to detail how even today designing a good API
requires a lot experience and effort by illustrating the example of
something as common place as the select() function call available in
sockets. The example focuses on the design decision surrounding the
Select() call in C#.

It comes under the heading "Bad APIs are easy" and goes on for about 4
pages about its problems.

The link to the article is here:
http://www.acmqueue.com/modules.php?...pid=488&page=1

What do the experts think?

Sep 28 '07 #3
On Sep 28, 8:16 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Socket.Select isn't meant to be a good, modern API, it is meant to emulate
Berkely Sockets' select() function. It looks like it does that quite well.
Hmmm... I would disagree with that. The .NET Select() adds the
following errors on top of the (already poor) design of the C version
of select():

- It does not allow the caller to wait in definitely (in .NET 1.1)

- It does not allow the caller to set a timeout longer than 35 minutes

- It makes it difficult to distinguish whether it returns because of a
timeout or because a
socket is read because the return type is void.

- It allows the caller to pass duplicate sockets because the
parameters a lists, not sets.

- It has ambiguous input and output values because any of the socket
lists can be null or
be an empty list.

So, as far as emulating the original is concerned, I think Select()
does quite a poor job.
The suggested improvements are a big step forward from select, but pale
horribly beside event-driven programming (WSAEventSelect or WSAAsyncSelect
or a completion port which in .NET means asynchronous BeginXYZ calls with a
WaitHandle or a completion callback delegate).
Of course the improvements I suggested are not the bees' knees of
Select() and, as I said in the article, the point wasn't to come up
with the ultimate version of select (other people have done that
already--I mentioned epoll() as an example); the point was to show how
a few seemingly small errors quickly pile up to cause a lot of grief.
Overall, Socket.Select isn't meant to be the .NET way, it's meant to ease
porting code that already uses BSD select().
Well, having done exactly that, namely ported code that already used
BSD select(), I can definitely state that .NET Select() fails to ease
porting such code quite spectacularly.

Cheers,

Michi.

Sep 28 '07 #4
On Sep 29, 12:18 am, "Michael Rubinstein"
<mSPAM_REMOVEr@m®ubinstein.comwrote:
The part of the article I liked
most, is where the author condemns the API forcing the developer using error
trapping for intercepting error that should not be anticipated.
There are tons of APIs around that throw exceptions when they
shouldn't. And, invariably, when they do, it causes grief for the
caller.
The author of the article calls the API design that encourages such
practices 'perverse', and I agree.
I don't recall using this term in that context. Not that I'd disagree
though. Asynchrnous reads from a .NET socket do exactly this: throw an
exception when for the expected outcome (namely, no data ready to
read), and return zero for the unexpected outcome (namely, connection
loss). Code that has to deal with this nonsense ends up looking very
contorted because of the mess of control flow.

Cheers,

Michi.

Sep 28 '07 #5
Michi,
>
I don't recall using this term in that context.
>
on page 5 you wrote:
(Another popular design flaw-namely, throwing exceptions for expected
outcomes-also causes inefficiencies because catching and handling exceptions
is almost always slower than testing a return value.)

This was what you wrote. What I ment, is that code like below - pasted
directly from VS2005 help for Socket class:

public static void Read_Callback(IAsyncResult ar){
StateObject so = (StateObject) ar.AsyncState;
Socket s = so.workSocket;

int read = s.EndReceive(ar);

if (read 0) {
so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0,
new
AsyncCallback(Async_Send_Receive.Read_Callback), so);
}
else{
if (so.sb.Length 1) {
//All of the data has been read, so displays it to the console
string strContent;
strContent = so.sb.ToString();
Console.WriteLine(String.Format("Read {0} byte from socket" +
"data = {1} ", strContent.Length, strContent));
}
s.Close();
}
}is guaranteed to crash when the connection is lost, or the other side
disconnects forcefully, unless the code is wrapped into try/catch. Under
Win32 Winsock there was no need for using error trapping when responding to
socket events because WSAAsyncSelect() takesthe handle of the window
receiving notifications. .NET Sockets do not offer any means to notify the
application when the socket is aboutto 'bite the dust'. So we are using
error trapping for handling predictable situations. The sad thing, it is
easy to get used to. Michael
"Michi Henning" <mi***@zeroc.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Sep 29, 12:18 am, "Michael Rubinstein"
<mSPAM_REMOVEr@m®ubinstein.comwrote:
The part of the article I liked
most, is where the author condemns the API forcing the developer using
error
trapping for intercepting error that should not be anticipated.
There are tons of APIs around that throw exceptions when they
shouldn't. And, invariably, when they do, it causes grief for the
caller.
The author of the article calls the API design that encourages such
practices 'perverse', and I agree.
I don't recall using this term in that context. Not that I'd disagree
though. Asynchrnous reads from a .NET socket do exactly this: throw an
exception when for the expected outcome (namely, no data ready to
read), and return zero for the unexpected outcome (namely, connection
loss). Code that has to deal with this nonsense ends up looking very
contorted because of the mess of control flow.

Cheers,

Michi.
Sep 29 '07 #6
On Sep 29, 2:26 pm, "Michael Rubinstein"
<mSPAM_REMOVEr@m®ubinstein.comwrote:
Michi,

I don't recall using this term in that context.

on page 5 you wrote:
(Another popular design flaw-namely, throwing exceptions for expected
outcomes-also causes inefficiencies because catching and handling exceptions
is almost always slower than testing a return value.)

This was what you wrote.
Right. I didn't say "perverse" there, although I would say that's an
apt description :-)
.NET Sockets do not offer any means to notify the
application when the socket is aboutto 'bite the dust'. So we are using
error trapping for handling predictable situations. The sad thing, it is
easy to get used to.
Yes, Depressing, really. Things like this are a clear indication that
the API design never ate his own dog food...

Cheers,

Michi.

Sep 29 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas Hervé | last post by:
My problem is not really python specific but as I do my implementation in python I hope someone here can help me. I have two programs that talk through a socket. Here is the code : <server>...
4
by: flupke | last post by:
Hi, I have a gui (made in wxPython) that enables a user to connect to a server and issue some commands. The problem occurs when i try to disconnect the client. It exits but it doesn't return to...
4
by: Bryan Olson | last post by:
Here's the problem: Suppose we use: import socket f = some_socket.makefile() Then: f.read() is efficient, but verbose, and incorrect (or at least does not play will with others);
12
by: Tonino | last post by:
I have been looking through the previous posts - but with my lack of knowledge on the whole tkinter subject - I have no clue what to look for ... SO - can anyone please help with this ...? I...
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
6
by: billiejoex | last post by:
Hi there. I'm setting up test suite for a project of mine. situations, if the socket is closed on the other end or not. I noticed that I can "detect" such state if a call to socket.read() returns...
12
by: Sophie000 | last post by:
I am implementing a peer-to-peer program: When a computer A receives “get file XXX” from stdio, it broadcasts (floods) a request packet to ask others. Both Computer B and C have the file so they...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.