Using DNS Service Discovery in Windows

DNS Service Discovery was written with cross-platform compatibility in mind. Therefore, all of the DNS Service Discovery API calls that are valid in OS X and iOS are also valid in Windows. The difference between the two platforms lies in how each handles run loops. The next two sections will explain what changes need to be made to write programs that take advantage of DNS Service Discovery in Windows. Before reading these sections, you’ll want to become familiar with the DNS Service Discovery API and Microsoft Foundation classes, if you are not already.

Windows Graphical User Interfaces

To properly incorporate DNS Service Discovery in a Windows graphical user interface, use the WinSock WSAAsyncSelect function. The WSAAsyncSelect function integrates socket-based network events into the Windows message loop. To use this in your Windows code, you should first create and initialize a DNSServiceRef object. Then, call the function WSAAsyncSelect to associate your DNSServiceRef object’s socket with the Windows message loop. WSAAsyncSelect requires four arguments: a socket to your DNSServiceRef object, a window to receive the message, a message to be sent when the event occurs, and a bitmask for the network events you are interested in. A simple example of this is provided below. In the example, you can see how to create a NULL DNSServiceRef object, initialize that reference with DNSServiceBrowse, and then add it to the work loop with WSAAsyncSelect.

// create blank DNSServiceRef
e = new ServiceHandlerEntry;
// initialize the DNSServiceRef for browsing
err = DNSServiceBrowse( &e->ref, 0, 0, e->type, NULL, BrowseCallBack, e );
// add browsing to the work loop with WSAAsyncSelect
//  where m_hWnd is the window, WM_PRIVATE_SERVICE_EVENT is the message and
//  FD_READ and FD_CLOSE are bitmasks for reading and closing sockets
err = WSAAsyncSelect((SOCKET) DNSServiceRefSockFD(e->ref),

Windows Command-Line Interfaces

Creating a Windows command-line program using DNS Service Discovery is similar to creating one for OS X or iOS. Windows, like OS X and iOS, has support for the select system call. This function is used to determine when results are available from the DNS Service Discovery API functions. More information about using the select loop with DNS Service Discovery is available in “Registering and Terminating a Service,” “Browsing for Network Services,” and “Resolving the Current Address of a Service.”