Home > Articles > Operating Systems, Server > Microsoft Windows Desktop

  • Print
  • + Share This
This chapter is from the book


Windows Store apps have the capability to communicate over lower-level networking protocols. The Windows Runtime provides built-in support for User Datagram Protocol (UDP),2 Transmission Control Protocol (TCP),3 Bluetooth RFCOMM,4 and the recent HTML5 WebSocket Protocol.5 Support for socket-based operations is provided through the types of the Windows.Networking.Sockets namespace. Sockets in general provide low-level network communications and enable real-time network notifications.


The WebSocket protocol was designed to be implemented in web browsers and web servers, and it is fully supported from Windows Store apps. Although it is part of the HTML5 group of specifications, it is an independent TCP protocol. Its main advantage is that it provides a way for the browser or Windows Store app to maintain a single connection with a server and send data both ways while keeping that connection open. The standard port for WebSockets is 80, the same one HTTP uses, which means it is less likely to be blocked by firewalls.

The WebSocketsExamples project for Chapter 10 demonstrates two APIs you can use from WinRT to take advantage of the WebSockets protocol. The example app leverages a server supplied by the WebSocket.org website that provides an “echo service.” This service, when connected to, echoes back any data sent to it. WebSockets are accessed using a standard URI, as declared in MainPage.xaml.cs:

private readonly Uri echoService =
    new Uri("ws://echo.websocket.org", UriKind.Absolute);

The MessageWebSocket class is an abstraction of the protocol that focuses on sending simple messages. A message is either read or written in a single operation, instead of being streamed continuously. It is also the class you must use to support UTF8 messages; the stream-based API supports only binary (although you can encode and decode the binary to and from UTF8, the MessageWebSocket class provides native support for this). To use any socket type within a Windows Store app, you must enable a networking capability such as Internet (Client).

The ButtonBase_OnClick method in the MainPage.xaml.cs file demonstrates how to use the MessageWebSocket class. After creating an instance of the class, set the type of the message (either binary or UTF8):

this.socket.Control.MessageType = SocketMessageType.Utf8;

You can also register for events that fire whenever a message is received and when the socket is closed. The socket uses underlying unmanaged resources, and you should dispose of it when you are done using it. The easiest way to do this is to call Dispose in the Closed event handler.

Initiate the connection by calling and waiting for ConnectAsync to complete:

await this.socket.ConnectAsync(echoService);

The example app accepts any message you type and sends it to the echo service. The message must be sent using the OutputStream property exposed by the socket. The easiest way to do this is to create an instance of a DataWriter to send the message. The DataWriter enables you to write various data types that it buffers until you call StoreAsync. This flushes the buffer to the underlying stream.

var writer = new DataWriter(this.socket.OutputStream);
await writer.StoreAsync();

Not all error messages for the socket are mapped to .NET Exception class instances. Instead, you must inspect the HResult of the underlying exception to determine what went wrong. Fortunately, the WebSocketError class provides a statc method that translates the result to the corresponding WebErrorStatus enumeration. The ToErrorMessage method returns a string with the original message and the enumeration value.

private static string ToErrorMessage(Exception ex)
    var status = WebSocketError.GetStatus(
    return string.Format("{0} ({1})", ex.Message, status);

The MessageReceived event is raised whenever a message is sent from the server to the client through the socket. In the example app, this should happen any time data is sent because the server echoes back the data. The event provides the socket that the information was received from with event arguments: You can inspect the message type (binary or UTF8) and open a reader or stream to access the message. In this example, the reader is set to use UFT8 encoding; then it obtains the message and displays it in the SocketMessageReceived event handler.

using (var reader = args.GetDataReader())
    reader.UnicodeEncoding = UnicodeEncoding.Utf8;
    var text = reader.ReadString(reader.UnconsumedBufferLength);
    this.Response.Text = text;

This is the simplest method for dealing with sockets that are designed to share messages. When you are using the socket to stream real-time information and you don’t necessarily have simple messages, you might want to use the StreamWebSocket implementation instead. It provides a continuous two-way stream for sending and receiving information. The example app uses the same echo service to stream prime numbers and echo them back to the display when you click the Start button.

You create and connect to a StreamWebSocket the same way as with a MessageWebSocket. You can also register for the Closed event. Instead of sending and receiving messages, however, the stream version expects you to interface directly with the input and output streams provided by the socket. The app starts a long-running Task encapsulated in the ComputePrimes method. It is passed the OutputStream of the socket. It iterates through positive integers and writes out any that are computed to be primes; then it delays for 1 second:

if (IsPrime(x))
    var array = Encoding.UTF8.GetBytes(string.Format(" {0} ", x));
    await outputStream.WriteAsync(array.AsBuffer());
    await Task.Delay(TimeSpan.FromSeconds(1));

If the integer is not a prime, it delays for a millisecond just to prevent hogging the CPU. Another long-running task receives the echo. It allocates a buffer, waits for data to arrive in the stream, and then reads and decodes the data.

var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead > 0)
    var text = Encoding.UTF8.GetString(buffer, 0, bytesRead);

This example also demonstrates that you can have multiple sockets open to the same server and port at once. You can run the example, click the button to start generating primes, and then use the message-based version to send and receive messages without interrupting the stream of prime numbers. Both methods for communicating with the socket simplify the amount of code you have to write by not worrying about the details of the underlying transport (TCP). When you need to manage a raw TCP connection, you can use the traditional sockets components.

UDP and TCP Sockets

UDP and TCP protocols have been around for decades. Many modern protocols, including HTTP, sit on top of these more low-level protocols (TCP is the transport used by both HTTP and the WebSocket protocol you learned to use in the previous section). Two main differences exist between UDP and TCP: UDP does not require a connection, and UDP does not require any special ordering of packets or chunks of data. As a result, TCP tends to be more reliable and useful for bidirectional communication, and UDP is used when faster transmission rates are required and the application understands how to deal with unordered data.

Examples of protocols that sit on top of UDP include Domain Name Service (DNS) and Simple Network Management Protocol (SNMP). Protocols that sit on top of TCP include HTTP and Simple Mail Transfer Protocol (SMTP). The UDP classes are all prefixed with Datagram and operate similarly to the TCP classes prefixed with StreamSocket. The API enables you to “connect” to either protocol and send or receive messages. This provides a consistent interface and approach to using each protocol. The main difference is that no specific “listener” service for the UDP implementation exists because a persistent connection is not needed. Instead, you simply create a socket, register for the event when a message is received, and then send data packets or process incoming data as needed.

The SocketsGame example provides a more comprehensive example of using a persistent TCP connection. Although the game starts a server to listen for incoming requests, it should be clear that you cannot use these types of connections for communication between Windows Store apps on the same machine. Network isolation prevents the loopback interface from allowing connections across processes. The only reason this works in the example project is that the client and server are hosted in the same process. The example should show how to spin up a server to listen when necessary (for example, the same type of connection can be used to host a service for a Bluetooth service that allows Bluetooth devices to connect), as well as act as a client for a server hosted on the Internet.

The game itself is a text-based adventure game. It creates a 10x10 matrix of rooms for 100 rooms total and randomly connects rooms and places trophies in the various rooms. The object of the game is to explore the rooms and collect trophies until all have been found. A rudimentary parser accepts commands such as “look,” “get,” “north,” and “inventory.” Instead of playing as a local game, however, the game is hosted on a socket; the app must connect as a client to issue commands and receive updates.

Two sockets are defined in MainPage.xaml.cs: a StreamSocketListener, which is the server that listens for and establishes connections to clients, and a StreamSocket, which emulates a client connecting to the server. The server provides several options to bind to a generic service and listen to all incoming connections, to bind to a specific address, or even to bind to a specific network adapter. The service name can be a local service name or a port, or it can remain empty to have a port assigned. If you are using the socket for Bluetooth (RFCOMM), use the Bluetooth service ID. In this example, the name is set to 21212 as a unique port for the game. Binding enables your app to use that specific port to listen for incoming requests. If another app has already bound to the specified service, an exception is thrown.

this.serverSocket = new StreamSocketListener();
this.serverSocket.ConnectionReceived +=
await this.serverSocket.BindServiceNameAsync(ServiceName);

As with Web Sockets, to understand errors thrown by the sockets API, use the GetStatus static method of the SocketError class, as shown in the GetErrorText method.

private static string GetErrorText(Exception ex)
    return string.Format("{0} ({1})", ex.Message,

When a connection is received, the server creates a persistent writer and reader for the connection (note that this example uses exactly one client, so only one writer and reader are used—if you are building a server to manage multiple connections, you need to spin up a new reader and writer for each unique connection).

if (serverWriter == null)
    serverWriter = new DataWriter(args.Socket.OutputStream);
    serverReader = new DataReader(args.Socket.InputStream);

The listener for the socket goes into an infinite loop waiting for messages. As messages are received, they are passed to the parser to interact with the game world, and the result is written back to the client. To facilitate communication over the socket, the messages are written with a special format. The size of the string in bytes is sent ahead of the string itself so that the reader can allocate the appropriate buffer size to process the incoming message. The SendString method encodes the text and sends it over the socket.

await writer.StoreAsync();

Listing 10.1 shows the GetStringFromReader method that receives the incoming data. It loads enough data to constitute an unsigned integer, processes the integer, and finally loads enough data to create a string based on the size that was passed in.

LISTING 10.1 Reading a String from the TCP Socket

private static async Task<string> GetStringFromReader(
    IDataReader reader)

    var sizeFieldCount = await reader.LoadAsync(sizeof(uint));
    if (sizeFieldCount != sizeof(uint))
        return string.Empty;
    var stringLength = reader.ReadUInt32();
    var actualStringLength = await reader.LoadAsync(stringLength);
    if (stringLength != actualStringLength)
        return string.Empty;
    var data = reader.ReadString(actualStringLength);
    return data;

Just as the server goes into an infinite loop after a connection is received, waits for instructions, and then returns a response, the client also starts a long-running task. On the UI thread, the Go_OnClick method is called whenever the user clicks the button to send the next command. The click handler simply sends the command to the socket and then forgets about it. The long-running ClientListener method waits to get the data from the server and then writes it for the end user to see.

Figure 10.2 shows a game in progress. At the top, you can see the server messages that involve receiving the incoming connection, receiving commands, and sending responses. The bottom is the client console for game play; it shows all the responses from the server and provides an input box for the user to type and send commands.


FIGURE 10.2 The example game played over a TCP socket

The provided example handles both client and server aspects for TCP connections. The RFCOMM for Bluetooth uses the same classes. Although UDP uses a different set of classes, the implementation is similar—the only difference is that you don’t create a persistent listener for managing connections because the protocol is stateless.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020