IO Schemes

In this section, we will show you how to use standard I/O schemes. These schemes use functions like select(), WSAAsyncSelect(), and WSAEventSelect(). The helper functions for WSAAsyncSelect() and WSAEventSelect() are WSACreateEvent(), WSAEnumNetworkEvents(), WSACloseEvent(), WSAResetEvent(), and WSAWaitForMultipleEvents(). We will also look at the WSAGetOverlappedResult() function for overlapped I/O operations. We will also explain when and how you would use these schemes. For now, we will introduce these functions briefly.

When you use the select() function for network event notification, the sockets that you use block by default. However, you can use ioctlsocket() (see Chapter 6, "Socket Options," for more details on this function) to make the sockets non-blocking. We will discuss non-blocking and blocking sockets later in the "To Block or Not to Block?" section.

The select() function is often known as a socket multiplex handler because it can handle sets of sockets for reading and writing. The maximum number of sockets that select() can handle is 64. To increase the number of sockets for an application using select(), you can use threads—one set of sockets for each thread. However, why make your life harder than it already is? Is there a more sane approach than using select()? Yes; Winsock has an asynchronous version of select() that takes advantage of Windows' messaging system. This is the WSAAsyncSelect() function.

Another function that is similar to WSAAsyncSelect() is WSAEventSelect(). The advantage of using WSAEventSelect() is that it does not require Windows handles. This is perfect for servers and daemons, as they do not usually require GUI front ends. Instead of using window handles, WSAEventSelect() uses the event object model for notification of network events. We will demonstrate the use of the WSAEventSelect() in a console application for a simple echo server. In any case, we would advise you to use WSAAsyncSelect() or WSAEvent-Select() over select() since these functions are easier to code and more robust.

Was this article helpful?

0 0

Post a comment