Microsoft Windows 8 and Windows Server 2012, the latest operating system and Windows makeover, were released in fall 2012. For programmers, many new APIs were included in the features added. Several of the new features focus on the venerable sockets interface, the basic network paradigm first introduced in Berkeley Software Distribution (BSD) UNIX in the early 1980s. Sockets still form the basis for networking on all of the major platforms including Windows, Mac OS, Linux, iOS, and Android.
You might want to consider these new Windows socket APIs for your next project when writing new apps or refactoring older applications for Windows. New Windows 8 features for sockets programming are found at both ends of the network spectrum:
- Window Runtime (WinRT) sockets: Used for low-level networking in Windows Store apps.
- High-performance sockets: Used in writing network servers for desktop apps that need low network latency and the highest performance.
- WebSockets: The other new networking APIs are socket-related, at least in name; these are extensions to the HTTP protocols for the creation of a special Web socket that is fully bidirectional once the connection is established, either endpoint can initiate or send packets.
Herein I provide a brief introduction to these new networking socket features available on Windows 8 and Windows Server 2012. The focus will be more on WinRT sockets, since these will appeal to a larger set of developers.
Sockets and Windows
The original Berkeley socket implementation developed in 1983 was very simple, based on the design of UNIX file I/O. A network socket became a special type of file handle. BSD networking functions were basic: socket
(open or create), connect
(for TCP), send
, recv
, and close
. Several types of sockets were supported including TCP (stream), UDP (datagram), and later UNIX domain sockets. For TCP servers and applications receiving UDP packets, the bind
and listen
functions would bind to a network address and listen for incoming packets. For use with UDP, there were the sendto
and recvfrom
functions. A few other functions were provided for getting and setting socket options, getting a host by name or address; and there were the select
and poll
functions to check on the state of a socket.
With the adoption of Windows Sockets (Winsock) in 1993, Microsoft added support for sockets to Windows. The initial Winsock 1.1 API was relatively modest with calls similar to BSD UNIX plus a number of Windows-specific extension functions. Over time, the Winsock API has grown larger and larger. The current reference documentation for Windows Sockets for desktop apps lists more than 140 functions and 80 structures along with several hundred IOCTLS and socket options. This doesn't include functions used by the Winsock Service Provider Interface (SPI). The existing Winsock API has become very large, complex, and challenging for new users.
The new socket APIs introduced with Windows 8/Windows Server 2012 provide much simpler, stripped-down APIs for sockets programming closer to the spirit of the original BSD sockets.
Windows 8: Two for the Money
Windows 8 and Windows Server 2012 introduce a new graphical user interface (GUI) that represents a major redesign of the Windows user experience. The changes are targeted primarily for touch-enabled devices, with tiles used to represent apps and new system navigation features (charms, settings, etc.). On traditional Intel/AMD x86/x64 hardware, Windows 8 can be thought of as an operating system for two different types of apps:
- Windows Store apps: New apps that run on Windows 8 only. These apps are limited to using the WinRT APIs and a few other APIs (some classes from the .NET framework along with a few desktop functions) that are exposed for use by Windows Store apps.
- Windows desktop apps: Traditional Windows apps that provide developers access to all of the Windows APIs except the WinRT APIs (unless the WinRT class has a special
DualApiPartitionAttribute
). These represent the traditional applications that ran on Windows 7 and older versions of the OS. If you use new Windows 8 desktop APIs, then the app will only be able to run on Windows 8 and Windows Server 2012 or later versions.
On ARM-based hardware (in the original Microsoft Surface tablet, for example), Windows 8 has been stripped down so only Windows Store apps can be installed and used. Windows 8 apps using WinRT are installed by purchasing and downloading them from the Microsoft App Store (many apps are free). Windows and app upgrades are also downloaded and installed from the Microsoft Store. Sideloading apps on these WinRT-based devices is mostly restricted, except when Visual Studio 2012 is installed. Sideloading allows developers to test apps they write before submitting them for publication on the Windows App Store.
(Note: The Windows App Store is not the Microsoft.com online store that sells versions of Windows, Office, other Microsoft software, and some hardware. There is also a separate Windows Phone App Store for purchasing and downloading apps for Windows phones.)
Windows Runtime and Sockets
The WinRT APIs used by Windows Store apps provide a set of managed APIs that are designed for several different languages and presentation schemes:
- JavaScript with HTML: Designed to appeal to traditional Web developers.
- C#/VB.NET with XAML: Designed for existing C#/VB.NET developers and others using managed code (Java developers).
- C++ with XAML: Designed for core C++ desktop and COM developers as well as others using similar languages (Objective-C developers).
Developers are free to choose whatever language and presentation they prefer. In fact, the same app can be written in any of these languages. Microsoft provides downloadable samples for Windows Store apps, many of which are implemented in multiple languages for illustration. For example, the downloadable StreamSocket sample and the DatagramSocket sample are implemented in all three language combinations.