Riverace logo

How to Avoid the Top 10 Sockets Programming Errors

A white paper for software development managers

Today’s trade journals are full of stories about the latest hot trends and technologies. SOA. Web services. AJAX. These are cool – if you’re a Web programmer.

But if you’re like most senior software developers or managers we’ve worked with, the real workhorse in your applications and systems is plain old Sockets  – the low-level connections that link applications running on a network (i.e.: winsock or Windows sockets, bsd sockets, linux sockets, or tcp sockets).

Your systems move financial data, audio/video, patient health information, billing records – pretty much anything and everything, text or binary. Lots of data must move efficiently with nary a Web server in sight.

You may need to run on Windows, Linux, and UNIX across different hardware types and sizes from data center servers and desktops to handheld devices and embedded hardware.

Given the demands of today’s business world, everything had better work right, all the time. If a mistake happens or your system crashes, your business not only loses money – its hard-earned reputation for quality also suffers, and you personally are on the hot seat for the mistake.

This white paper will show you the 10 most common errors developers make when programming Sockets – especially Winsock run time errors! -- and how you can avoid them.

The 10 Most Common Sockets Programming Errors

Much of the cost of software development is in the testing and maintenance (fixing the bugs) stages.

Although developers know about socket programming and run-time errors and where they’re most likely to occur, it’s proven very difficult to avoid certain classes of Socket programming errors -- and those commonly encountered when programming Sockets for applications such as Windows, Linux, or Unix are among the thorniest.

Why is this? The reasons Sockets programming is very error-prone can be grouped into three categories:

  1. It’s too easy to do the wrong thing.
  2. The application program interface (API) is too complex.
  3. Despite Sockets’ ubiquity, it’s still not 100% portable.

To top it off, most of the Socket errors can’t be caught by the compiler, forcing developers to find and resolve these errors during testing, a far more costly effort than if the errors could be detected at compile time.

As an illustration, consider the following simple function. This code will successfully compile and link on most systems, yet it contains the ten most commonly made Sockets errors. Can you identify them?


Like what you’ve read so far? If you would like to read this Sockets Programming Errors white paper in its entirety, simply fill out the brief form below. You’ll be taken to a new page that includes a link to the white paper PDF.

*
*
*
*
 
 

Sockets White Paper cover

Winsock Error Example

11   int n_handle;
12   // Create the local endpoint.
13   int s_handle = socket (PF_UNIX,
       SOCK_DGRAM, 0);
14   if (s_handle == -1) return -1;

Use of nonportable handle datatype
Although these lines look harmless enough, they’re fraught with peril. This code isn't portable to Windows Sockets (WinSock) platforms, where socket handles are type SOCKET, not type int. Moreover, WinSock failures are indicated via a non-standard macro called INVALID_SOCKET_HANDLE rather than by returning -1. Other bugs exist in this section of the code fragment, but aren’t obvious until we examine the rest of the function.

 

 

About Riverace Corporation

Riverace Corporation is the premier provider of ACE – the ADAPTIVE Communication Environment. A modern, high-performance framework and C++ class library, the ACE toolkit offers classes designed to resolve the complexities inherent in Sockets programming. And, ACE is easily portable to a wide range of today’s popular computing programs – from handheld devices to supercomputers.

Home | About Us | Site Map | Privacy Policy | Contact Us | ©2005-2007 Riverace Corporation