Sockets
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.
WebSockets
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); writer.WriteString(this.Text.Text); 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( ex.GetBaseException().HResult); 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.DispatchTextToPrimes(text); }
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 += this.ServerSocketConnectionReceived; 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, SocketError.GetStatus(ex.GetBaseException().HResult)); }
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.
writer.WriteUInt32(writer.MeasureString(text)); writer.WriteString(text); 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.