SlideShare a Scribd company logo
Consulting/Training
Windows 8.1 Sockets
Consulting/Training
consulting
Wintellect helps you build better software,
faster, tackling the tough projects and solving
the software and technology questions that
help you transform your business.
 Architecture, Analysis and Design
 Full lifecycle software development
 Debugging and Performance tuning
 Database design and development
training
Wintellect's courses are written and taught by
some of the biggest and most respected names
in the Microsoft programming industry.
 Learn from the best. Access the same
training Microsoft’s developers enjoy
 Real world knowledge and solutions on
both current and cutting edge
technologies
 Flexibility in training options – onsite,
virtual, on demand
Founded by top experts on Microsoft – Jeffrey Richter, Jeff Prosise, and John Robbins – we pull
out all the stops to help our customers achieve their goals through advanced software-based
consulting and training solutions.
who we are
About Wintellect
Consulting/Training
 Sockets, Simplified
 WebSockets
 UDP and TCP Sockets
 Proximity / NFC “Tap to Connect”
 Bluetooth and Wi-Fi Direct
 Recap
Agenda
Consulting/Training
 Covers full WinRT API
 Over 80 example
projects
 This presentation is
based on Chapter 10
 Full source online at
http://winrtexamples
.codeplex.com/
 Book available at
http://bit.ly/winrtxmpl
WinRT by Example
Consulting/Training
Sockets, Simplified
Byte
Consulting/Training
Sockets, Simplified
Byte Array
Consulting/Training
Sockets, Simplified
Stream
 Length (maybe)
 Position
 Read
 Write
 Seek
Consulting/Training
Sockets Are Specialized Streams
Consulting/Training
 Berkeley sockets released with Unix in 1983 (owned by
AT&T at the time) for “IPC” while I was writing my first
Commodore 64 programs in 6502 assembly
 Open licensing in 1989 (I had finally moved to IBM PC and
MS-DOS)
 POSIX API (Portable Operating System Interface) released
in 1988
 Windows Sockets API (WSA or Winsock) released in 1992 (I
graduated high school and discovered the Internet in
college) and later implemented for Windows
 Another F Ancillary Function Driver (AFD.sys) still exists to
this day
Sockets: A Brief History
Consulting/Training
Sockets Are Specialized Streams
Consulting/Training
Sockets Are Specialized Streams
Consulting/Training
 A way to communicate between end points
 Usually operate on streams, which are
abstractions over buffers that contain bytes
 But can be a datagram that has no connection
 A socket connection has two distinct end points
 An end point is a combination of an address and
a port
 End points can be hosted on the same machine,
or different machines
Sockets Recap
Consulting/Training
int main(void)
{
struct sockaddr_in stSockAddr;
int Res;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == SocketFD)
{
perror("cannot create socket");
exit(EXIT_FAILURE);
}
memset(&stSockAddr, 0, sizeof(stSockAddr));
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1100);
Res = inet_pton(AF_INET, "192.168.1.1", &stSockAddr.sin_addr);
(void) shutdown(SocketFD, SHUT_RDWR);
close(SocketFD);
return EXIT_SUCCESS;
}
“The Old Days” (still valid 30 yrs. later)
Consulting/Training
 Sit on top of TCP (you’ll learn more about TCP
later)
 Operate over ports 80/443 by default (keeps
firewalls happy)
 Use HTTP for the initial “handshake”
 Provide full-duplex communications
 WinRT implementation allows for message-
based or stream-based
WebSockets
Consulting/Training
GET /info HTTP/1.1
HOST: 1.2.3.4
Upgrade: websocket
Connection: Upgrade
Origin: http://jeremylikness.com/
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
WebSocket Handshake
Placing my call …
Accepting ….
Now it’s streams from here on out….
Consulting/Training
(a prime example… source code: http://bit.ly/1i5vB9p)
WebSockets
Consulting/Training
 Both are communication protocols for delivering
octet streams (bytes!)
 UDP is fire-and-forget (connectionless)
 Individual messages sent, i.e. “datagrams”
 Fast, lots of questions: DNS, DHCP, SNMP
 TCP is connection-oriented
 TCP is stream-based
 Slower, but reliable: HTTP, FTP, SMTP, Telnet
UDP and TCP
Consulting/Training
this.serverSocket = new StreamSocketListener();
this.serverSocket.ConnectionReceived +=
this.ServerSocketConnectionReceived;
await this.serverSocket.BindServiceNameAsync(ServiceName);
// can both read and write messages (full-duplex)
if (serverWriter == null)
{
serverWriter = new DataWriter(args.Socket.OutputStream);
serverReader = new DataReader(args.Socket.InputStream);
}
Server Listens for Connections
Consulting/Training
var hostName = new HostName("localhost");
this.clientSocket = new StreamSocket();
await this.clientSocket.ConnectAsync(hostName, ServiceName);
clientWriter = new DataWriter(this.clientSocket.OutputStream);
clientReader = new DataReader(this.clientSocket.InputStream);
// both client and server require long-running tasks to wait for
// messages and send them as needed
while (true)
{
var data = await GetStringFromReader(clientReader);
// do something with the data
}
Client “Dials In” to Chat
Consulting/Training
(a true adventure… source code: http://bit.ly/1j77rP3)
TCP Sockets
Consulting/Training
 Near Field Communications (NFC) is a standard for extremely low powered
communications typically very slow and over a very small distance, between devices
and/or smart tags
 NFC is capable of sending small bursts of information. However, “tap to connect” is a
powerful mechanism for establishing a hand-shake to open a stream over other,
longer range and faster protocols like Bluetooth and Wi-Fi Direct
 Windows Runtime exposes the Proximity APIs that provide a unified way to discover
nearby devices and a protocol-agnostic mechanism to connect
 Literally you can discover, handshake, etc. and WinRT will “hand-off” a socket that is
ready to use (you won’t even know if it is over Wi-Fi Direct or Bluetooth)
 Bluetooth = short wavelength wireless technology
 Wi-Fi Direct = peer to peer over Wi-Fi (wireless cards without requiring a wireless
access point or router)
NFC and Proximity
Consulting/Training
this.proximityDevice = ProximityDevice.GetDefault();
this.proximityDevice.DeviceArrived +=
this.ProximityDeviceDeviceArrived;
this.proximityDevice.DeviceDeparted +=
this.ProximityDeviceDeviceDeparted;
PeerFinder.ConnectionRequested +=
this.PeerFinderConnectionRequested;
PeerFinder.Role = PeerRole.Peer;
PeerFinder.Start();
var peers = await PeerFinder.FindAllPeersAsync();
var socket = await
PeerFinder.ConnectAsync(this.SelectedPeer.Information);
Proximity APIs
NFC device and events
Peer Finder (listen and browse)
Browse for peers
Connect to peer
Consulting/Training
1. Try to get Proximity Device (NFC)
2. If exists, register to enter/leave events (NFC)
3. If it supports triggers (i.e. tap) register for connection
state change event (NFC)
4. Otherwise browse for peers (Wi-Fi Direct, Bluetooth)
5. When a peer is found, send a connection request
6. Peer must accept the connection request
7. In any scenario, once the connection exists you are
passed a stream socket and are free to communicate
Steps for Proximity
Consulting/Training
Source code: http://bit.ly/1jlnGbB
Proximity
Consulting/Training
 WebSockets API for simplified WebSockets
 Sockets API for TCP, UDP, Bluetooth, Wi-Fi Direct
 Windows 8.1 is capable of dialing out to any
address and port
 As a server, most practical example is listening
for Bluetooth connections
 The security sandbox prevents using sockets for
inter-app communications
Recap
Consulting/Training
Subscribers Enjoy
 Expert Instructors
 Quality Content
 Practical Application
 All Devices
Wintellect’s On-Demand
Video Training Solution
Individuals | Businesses | Enterprise Organizations
WintellectNOW.com
Authors Enjoy
 Royalty Income
 Personal Branding
 Free Library Access
 Cross-Sell
Opportunities
Try It Free!
Use Promo Code:
LIKNESS-13
Consulting/Training
http://winrtexamples.codeplex.com/
http://bit.ly/winrtxmpl
Questions?

More Related Content

PPTX
The Windows Runtime and the Web
PPTX
Enterprise TypeScript
PPTX
My XML is Alive! An Intro to XAML
PPTX
WinRT and the Web: Keeping Windows Store Apps Alive and Connected
PPT
Struts 2-overview2
PDF
MVC in PHP
PDF
Principles of MVC for PHP Developers
PPTX
1. Spring intro IoC
 
The Windows Runtime and the Web
Enterprise TypeScript
My XML is Alive! An Intro to XAML
WinRT and the Web: Keeping Windows Store Apps Alive and Connected
Struts 2-overview2
MVC in PHP
Principles of MVC for PHP Developers
1. Spring intro IoC
 

What's hot (20)

ODP
Play with Angular JS
PPTX
Angular jS Introduction by Google
 
PDF
Web Development with Delphi and React - ITDevCon 2016
PPT
PHP Frameworks and CodeIgniter
PPTX
Java Technology
PDF
JavaCro'14 - Consuming Java EE Backends in Desktop, Web, and Mobile Frontends...
ODP
A Good PHP Framework For Beginners Like Me!
PPTX
04 managing the database
PPT
Php Frameworks
PPTX
MVVM - Model View ViewModel
PPT
Introduction To Code Igniter
PPT
Dev212 Comparing Net And Java The View From 2006
PDF
Hibernate Presentation
PPTX
MVC Frameworks for building PHP Web Applications
PPTX
Java script performance tips
PDF
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
PPTX
Introduction to web compoents
PPTX
MVVM Design Pattern NDC2009
PPTX
React JS Interview Question & Answer
PPSX
Spring - Part 4 - Spring MVC
Play with Angular JS
Angular jS Introduction by Google
 
Web Development with Delphi and React - ITDevCon 2016
PHP Frameworks and CodeIgniter
Java Technology
JavaCro'14 - Consuming Java EE Backends in Desktop, Web, and Mobile Frontends...
A Good PHP Framework For Beginners Like Me!
04 managing the database
Php Frameworks
MVVM - Model View ViewModel
Introduction To Code Igniter
Dev212 Comparing Net And Java The View From 2006
Hibernate Presentation
MVC Frameworks for building PHP Web Applications
Java script performance tips
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
Introduction to web compoents
MVVM Design Pattern NDC2009
React JS Interview Question & Answer
Spring - Part 4 - Spring MVC
Ad

Viewers also liked (8)

PPTX
Angular from a Different Angle
PPTX
Advanced AngularJS Tips and Tricks
PPTX
Let's Build an Angular App!
PPTX
Angle Forward with TypeScript
PPTX
C# Async/Await Explained
PPTX
Single Page Applications: Your Browser is the OS!
PPTX
Back to the ng2 Future
PPTX
Cross-Platform Agile DevOps with Visual Studio Team Services
Angular from a Different Angle
Advanced AngularJS Tips and Tricks
Let's Build an Angular App!
Angle Forward with TypeScript
C# Async/Await Explained
Single Page Applications: Your Browser is the OS!
Back to the ng2 Future
Cross-Platform Agile DevOps with Visual Studio Team Services
Ad

Similar to Windows 8.1 Sockets (20)

PPTX
Web sockets are not just for web browsers
PPT
App layer
PDF
A new interface between smart device and web using html5 web socket and qr code
PDF
API Design and WebSocket
PPT
Application Layer
PDF
Be IT Conference 2015 | MentorMate - Adding multiplayer to your mobile game: ...
PPT
Networking and socket
PPTX
WebSockets in JEE 7
PPTX
WebSocket protocol
PPT
Chapter - 1 Introduction to networking (3).ppt
PPTX
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
PDF
Computer Networks Module 1-part 1.pdf
PDF
Internet Programming With Python Presentation
PPTX
Understanding the Transport Layer: MUX, DEMUX, Process-to-Process Delivery, T...
PDF
Application layer jain
KEY
P2P on the local network
PDF
Computer Network notes Application Layer.pdf
PDF
Chapter 25.pdfassssssssssssssvjvvdvdvvdv
PDF
Understanding computer networks
PDF
Real-time applications with sockets and websockets. Introduction to Smartfoxs...
Web sockets are not just for web browsers
App layer
A new interface between smart device and web using html5 web socket and qr code
API Design and WebSocket
Application Layer
Be IT Conference 2015 | MentorMate - Adding multiplayer to your mobile game: ...
Networking and socket
WebSockets in JEE 7
WebSocket protocol
Chapter - 1 Introduction to networking (3).ppt
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Computer Networks Module 1-part 1.pdf
Internet Programming With Python Presentation
Understanding the Transport Layer: MUX, DEMUX, Process-to-Process Delivery, T...
Application layer jain
P2P on the local network
Computer Network notes Application Layer.pdf
Chapter 25.pdfassssssssssssssvjvvdvdvvdv
Understanding computer networks
Real-time applications with sockets and websockets. Introduction to Smartfoxs...

Recently uploaded (20)

PDF
Modernizing your data center with Dell and AMD
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
A Presentation on Artificial Intelligence
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Modernizing your data center with Dell and AMD
The Rise and Fall of 3GPP – Time for a Sabbatical?
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
A Presentation on Artificial Intelligence
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Per capita expenditure prediction using model stacking based on satellite ima...

Windows 8.1 Sockets

  • 2. Consulting/Training consulting Wintellect helps you build better software, faster, tackling the tough projects and solving the software and technology questions that help you transform your business.  Architecture, Analysis and Design  Full lifecycle software development  Debugging and Performance tuning  Database design and development training Wintellect's courses are written and taught by some of the biggest and most respected names in the Microsoft programming industry.  Learn from the best. Access the same training Microsoft’s developers enjoy  Real world knowledge and solutions on both current and cutting edge technologies  Flexibility in training options – onsite, virtual, on demand Founded by top experts on Microsoft – Jeffrey Richter, Jeff Prosise, and John Robbins – we pull out all the stops to help our customers achieve their goals through advanced software-based consulting and training solutions. who we are About Wintellect
  • 3. Consulting/Training  Sockets, Simplified  WebSockets  UDP and TCP Sockets  Proximity / NFC “Tap to Connect”  Bluetooth and Wi-Fi Direct  Recap Agenda
  • 4. Consulting/Training  Covers full WinRT API  Over 80 example projects  This presentation is based on Chapter 10  Full source online at http://winrtexamples .codeplex.com/  Book available at http://bit.ly/winrtxmpl WinRT by Example
  • 7. Consulting/Training Sockets, Simplified Stream  Length (maybe)  Position  Read  Write  Seek
  • 9. Consulting/Training  Berkeley sockets released with Unix in 1983 (owned by AT&T at the time) for “IPC” while I was writing my first Commodore 64 programs in 6502 assembly  Open licensing in 1989 (I had finally moved to IBM PC and MS-DOS)  POSIX API (Portable Operating System Interface) released in 1988  Windows Sockets API (WSA or Winsock) released in 1992 (I graduated high school and discovered the Internet in college) and later implemented for Windows  Another F Ancillary Function Driver (AFD.sys) still exists to this day Sockets: A Brief History
  • 12. Consulting/Training  A way to communicate between end points  Usually operate on streams, which are abstractions over buffers that contain bytes  But can be a datagram that has no connection  A socket connection has two distinct end points  An end point is a combination of an address and a port  End points can be hosted on the same machine, or different machines Sockets Recap
  • 13. Consulting/Training int main(void) { struct sockaddr_in stSockAddr; int Res; int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (-1 == SocketFD) { perror("cannot create socket"); exit(EXIT_FAILURE); } memset(&stSockAddr, 0, sizeof(stSockAddr)); stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(1100); Res = inet_pton(AF_INET, "192.168.1.1", &stSockAddr.sin_addr); (void) shutdown(SocketFD, SHUT_RDWR); close(SocketFD); return EXIT_SUCCESS; } “The Old Days” (still valid 30 yrs. later)
  • 14. Consulting/Training  Sit on top of TCP (you’ll learn more about TCP later)  Operate over ports 80/443 by default (keeps firewalls happy)  Use HTTP for the initial “handshake”  Provide full-duplex communications  WinRT implementation allows for message- based or stream-based WebSockets
  • 15. Consulting/Training GET /info HTTP/1.1 HOST: 1.2.3.4 Upgrade: websocket Connection: Upgrade Origin: http://jeremylikness.com/ HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade WebSocket Handshake Placing my call … Accepting …. Now it’s streams from here on out….
  • 16. Consulting/Training (a prime example… source code: http://bit.ly/1i5vB9p) WebSockets
  • 17. Consulting/Training  Both are communication protocols for delivering octet streams (bytes!)  UDP is fire-and-forget (connectionless)  Individual messages sent, i.e. “datagrams”  Fast, lots of questions: DNS, DHCP, SNMP  TCP is connection-oriented  TCP is stream-based  Slower, but reliable: HTTP, FTP, SMTP, Telnet UDP and TCP
  • 18. Consulting/Training this.serverSocket = new StreamSocketListener(); this.serverSocket.ConnectionReceived += this.ServerSocketConnectionReceived; await this.serverSocket.BindServiceNameAsync(ServiceName); // can both read and write messages (full-duplex) if (serverWriter == null) { serverWriter = new DataWriter(args.Socket.OutputStream); serverReader = new DataReader(args.Socket.InputStream); } Server Listens for Connections
  • 19. Consulting/Training var hostName = new HostName("localhost"); this.clientSocket = new StreamSocket(); await this.clientSocket.ConnectAsync(hostName, ServiceName); clientWriter = new DataWriter(this.clientSocket.OutputStream); clientReader = new DataReader(this.clientSocket.InputStream); // both client and server require long-running tasks to wait for // messages and send them as needed while (true) { var data = await GetStringFromReader(clientReader); // do something with the data } Client “Dials In” to Chat
  • 20. Consulting/Training (a true adventure… source code: http://bit.ly/1j77rP3) TCP Sockets
  • 21. Consulting/Training  Near Field Communications (NFC) is a standard for extremely low powered communications typically very slow and over a very small distance, between devices and/or smart tags  NFC is capable of sending small bursts of information. However, “tap to connect” is a powerful mechanism for establishing a hand-shake to open a stream over other, longer range and faster protocols like Bluetooth and Wi-Fi Direct  Windows Runtime exposes the Proximity APIs that provide a unified way to discover nearby devices and a protocol-agnostic mechanism to connect  Literally you can discover, handshake, etc. and WinRT will “hand-off” a socket that is ready to use (you won’t even know if it is over Wi-Fi Direct or Bluetooth)  Bluetooth = short wavelength wireless technology  Wi-Fi Direct = peer to peer over Wi-Fi (wireless cards without requiring a wireless access point or router) NFC and Proximity
  • 22. Consulting/Training this.proximityDevice = ProximityDevice.GetDefault(); this.proximityDevice.DeviceArrived += this.ProximityDeviceDeviceArrived; this.proximityDevice.DeviceDeparted += this.ProximityDeviceDeviceDeparted; PeerFinder.ConnectionRequested += this.PeerFinderConnectionRequested; PeerFinder.Role = PeerRole.Peer; PeerFinder.Start(); var peers = await PeerFinder.FindAllPeersAsync(); var socket = await PeerFinder.ConnectAsync(this.SelectedPeer.Information); Proximity APIs NFC device and events Peer Finder (listen and browse) Browse for peers Connect to peer
  • 23. Consulting/Training 1. Try to get Proximity Device (NFC) 2. If exists, register to enter/leave events (NFC) 3. If it supports triggers (i.e. tap) register for connection state change event (NFC) 4. Otherwise browse for peers (Wi-Fi Direct, Bluetooth) 5. When a peer is found, send a connection request 6. Peer must accept the connection request 7. In any scenario, once the connection exists you are passed a stream socket and are free to communicate Steps for Proximity
  • 25. Consulting/Training  WebSockets API for simplified WebSockets  Sockets API for TCP, UDP, Bluetooth, Wi-Fi Direct  Windows 8.1 is capable of dialing out to any address and port  As a server, most practical example is listening for Bluetooth connections  The security sandbox prevents using sockets for inter-app communications Recap
  • 26. Consulting/Training Subscribers Enjoy  Expert Instructors  Quality Content  Practical Application  All Devices Wintellect’s On-Demand Video Training Solution Individuals | Businesses | Enterprise Organizations WintellectNOW.com Authors Enjoy  Royalty Income  Personal Branding  Free Library Access  Cross-Sell Opportunities Try It Free! Use Promo Code: LIKNESS-13