473,795 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6594

"Dilip" <rd*****@lycos. comwrote in message
news:11******** ************@19 g2000hsx.google groups.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******** ************@19 g2000hsx.google groups.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.no spamwrote:
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.com wrote:
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(I AsyncResult ar){
StateObject so = (StateObject) ar.AsyncState;
Socket s = so.workSocket;

int read = s.EndReceive(ar );

if (read 0) {
so.sb.Append(En coding.ASCII.Ge tString(so.buff er, 0, read));
s.BeginReceive( so.buffer, 0, StateObject.BUF FER_SIZE, 0,
new
AsyncCallback(A sync_Send_Recei ve.Read_Callbac k), 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.WriteLi ne(String.Forma t("Read {0} byte from socket" +
"data = {1} ", strContent.Leng th, 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.co mwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
On Sep 29, 12:18 am, "Michael Rubinstein"
<mSPAM_REMOVEr@ m®ubinstein.com wrote:
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.com wrote:
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
2655
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> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.settimeout(5.0)
4
2432
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 the prompt. I have to push Ctrl-C in order to have it exit completely. The GUI is closed though. This is a piece of code from the main class that connects to the server and starts a thread that handles the connection: =======================...
4
11780
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
6170
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 have a python server that when it gets a connection from a client - it sends data back - quite a bit of it - in discreet parts - around 1024 byte chunks ...
5
4232
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 it seems to stay in the application scope, but the paranoid side of me thinks I may be missing something... any confirmation would be helpful.
13
2368
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 (replacement for PHP's include() statement). My problem is, in PHP if you open a database connection it's always in scope for the duration of the script. Even if you use an abstraction layer ($db = DB::connect(...)) you can `global $db` and bring...
5
12806
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? I thought about putting a boolean in there, but then what if it's still waiting for an incoming...
6
15616
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 0 but it seems a little poor to me. :-\ Is there a reliable way to test such socket 'state'?
12
14573
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 send back responses. Computer A pick up B and sends confirm to B. at the same time, it creates a server socket to listen to connection. After Compute B receives the confirm from A, it creates a client socket to connects to A. I set timeouts to both...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.