23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

socketsSample.displayStatus("Client sent: " + string + ".");<br />

}, onError);<br />

The DataWriter.storeAsync call is what actually writes the data to the stream in the socket. If you set<br />

a breakpoint here and on both messagereceived event handlers, you’ll then see that storeAsync<br />

generates a message to the server side, hitting onServerMessageReceived in js/startListener.js. This will<br />

then write the message back to the socket, which will hit onMessage-Received in js/connectToListener.js,<br />

which displays the message. (And to complete the process, Scenario 4 gives you a button to call the<br />

socket’s close method.)<br />

The sample does everything with the same app on localhost to make it easier to see how the process<br />

works. Typically, of course, the server will be running on another machine entirely, but the steps of<br />

setting up a listener apply just the same. As noted in Chapter 13, localhost connections work only on a<br />

machine with a developer license and will not work for apps acquired through the Windows Store.<br />

Stream Sockets<br />

In contrast to datagram sockets, streaming data over sockets uses the Transmission Control Protocol<br />

(TCP). The hallmark of TCP is accurate and reliable delivery—it guarantees that the bytes received are<br />

the same as the bytes that were sent: when a packet is sent across the network, TCP will attempt to<br />

retransmit the packet if there are problems along the way. This is why it’s part of TCP/IP, which gives us<br />

the World Wide Web, email, file transfers, and lots more. HTTP, SMTP, and the Session Initiation<br />

Protocol (SIP) are also built on TCP. In all cases, clients and servers just see a nice reliable stream of data<br />

flowing from one end to the other.<br />

Unlike datagram sockets, for which we have a single class in WinRT for both sides of the relationship,<br />

stream sockets are more distinctive to match the unique needs of the client and server roles. On the<br />

client side is Windows.Networking.Sockets.StreamSocket; on the server it’s StreamSocketListener.<br />

Starting with the latter, the StreamSocketListener object looks quite similar to the DatagramSocket<br />

we’ve just covered in the previous section, with these methods, properties, and events:<br />

• information Provides a StreamSocketListenerInformation object containing a localPort<br />

string.<br />

• control Provides a StreamSocketListenerControl object with a qualityOfService property.<br />

• connectionreceived An event that’s fired when a connection is made to the listener. Its event<br />

arguments are a StreamSocketListenerConnectionReceivedEventArgs that contains a single<br />

property, socket. This is the StreamSocket for the client, in which is an outputStream property<br />

where the listener can obtain the data stream.<br />

• bindEndpointAsync and bindServiceNameAsync Binds the listener to a HostName and service<br />

name, or binds just a service name.<br />

close Terminates connections and aborts pending operations.<br />

680

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!