Yes it’s been while…but also meandering onto Winsock2

..since my last post, but then there’s been lots of changes just recently.

I’ve left Brussels temporarily while waiting for my transfer to the Warrington office to complete and am now working at home whilst also waiting for my son to arrive………..he seems to have inherited my sense of time keeping.

But anyway this is more a post about work,  I figured it was time to write something about my latest project.

So, I have to write an application which communicates with a hardware security module, the nature of which I won’t go into except that it accepts and produces cryptographic data of an application specific nature.

The HSM communicates over a TCP/IP connection, and exposes a logical command<>response messaging interface (behaviour). The communications link between the device and it’s clients is insecure in terms of it’s implementation, security being inferred upon it by virtue of it’s physical location.

The first goal was therefore to develop a socket communication framework, well layer is perhaps a better word. I don’t like frameworks, they tend to try to be all encompassing, and require too much integration into their client applications. So I needed to develop a sockets communications layer, based on a Win32 platform, therefore Winsock, Winsock2 actually, was the choice.

Firstly I have alot of experience with this API, and secondly using the Winsock2 API allowed me to address 1 specific application design requirement. That is the ability to interrupt a pending or current socket operation; Recv, Send or Connect, and to implement any of these operations with timeout characteristics.

Actually the 2nd requirement (timeouts) is possible with the berkley-style socket interface implemented by Winsock2 (send(…) / recv(…) etc) since Winsock2 correctly implements the SO_SNDTIMEO and SO_RCVTIMEO socket options. The 1st requirement (interrupts) I’ve never been able to find a good solution for, using the berkley-style interface.

Ok so whats this about Winsock2 and berkley-style interfaces?

Well the sockets API provided by MS Winsock/Winsock2 is an API based on the Berkley Sockets API originally developed with/by/for Unix eons ago, and it’s been with us ever since. The vast majority of C/C++ socket code you’ll see is written using this API, which exposes functions like:-







So if those functions ring a bell, you’ll know what I mean. Now, you can do alot with this interface, but the Win32 platforms provides alot of features, services etc which this berkley-style interface doesn’t address, and in some cases the berkley-style API (too me anyway) just doesn’t seem to fit right, the select(…) function being my favourite. So what MS did was to come up with Winsock2 which provided the same berkley-style API as did Winsock1 but also with alot of MS specific extensions (they’re good at that), which allow more seemless integration of socket communication into a Win32 application and which also feel more natural to a Windows developer.

This “Windows” style interface provided by Winsock2 exposes functions which begin with WSA, and which are sometimes named after their berkley-style forerunners: –





but which also provides others which bear little or no relation to anything in the berkley API. The MS extension in Winsock2 also provide alternate methods of communication; network events based, overlapped, non-overlapped, non-blocking, overlapped with IO completion ports and so on. You the developer must decided which method is best for the application, and also whether you’ll use the berkley-style or MS extensions as the basis for your development.

This can sometimes be an easy decision, especially as the API’s can be used interchangeably when you know what you are doing, at least.

And it is the MS extensions which I have used previously and which will now also allow me to address my application design requirements.

If you’re interested CodeProject has some very good articles on developing socket applications, my personal favourites being the articles by Len Holgate:-

A reusable, high performance, socket server class – Part 1

A reusable, high performance, socket server class – Part 2

A reusable, high performance, socket server class – Part 3

Handling multiple pending socket read and write operations

A high performance TCP/IP socket server COM component for VB

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

2 thoughts on “Yes it’s been while…but also meandering onto Winsock2

  1. How can you interrupt ‘select’ not using timeouts. I am looking for using a pipe and writing a character in a signal handler routine. The call to select would be also looking for activity on the pipe and will return. I will then know that a signal handler had triggered the return from select. Unfortunately, select on win32 does not like pipe/file handles. I tried emulating select using Waitformultipleobjects (by trying to get the underlying handle to socket fd but failed in get_osfhandle). Is there a way out for this?

  2. It’s been a long time since I did any work with sockets, but the only way I was able to successfully implement “interrupts” during socket communication was to use a combination of overlapped/network events techniques obviously using wsawaitformultipleobjects and using a manual Win32 event synch objects to wrap the interrupt signal.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.