The document discusses network messaging over Bonjour connections. It identifies three ways to separate network messages from one another: 1) Making all messages the same length, 2) Appending a marker to each message, or 3) Sending a header with length information for each message. The document focuses on building a native iOS application that allows clients to publish and browse for servers over Bonjour, and exchange data between connected clients using stream sockets. Key classes discussed are Server, Connection, and ServerBrowser classes.
4. Few words about me
Nicholas Valbusa
@squallstar
Thursday, May 24, 12
5. Few words about me
Nicholas Valbusa
@squallstar
Web Developer since 2004
Thursday, May 24, 12
6. Few words about me
Nicholas Valbusa
@squallstar
Web Developer since 2004
Mobile Developer since 2010
Thursday, May 24, 12
7. Few words about me
Nicholas Valbusa
@squallstar
Web Developer since 2004
Mobile Developer since 2010
WHYMCA 2011: Webkit meets Native development
Thursday, May 24, 12
16. We’ll talk about
• TCP/IP
• Apple Bonjour, uPNP
Thursday, May 24, 12
17. We’ll talk about
• TCP/IP
• Apple Bonjour, uPNP
• Socket, Stream, Buffers
Thursday, May 24, 12
18. We’ll talk about
• TCP/IP
• Apple Bonjour, uPNP
• Socket, Stream, Buffers
• Create and publish a server
Thursday, May 24, 12
19. We’ll talk about
• TCP/IP
• Apple Bonjour, uPNP
• Socket, Stream, Buffers
• Create and publish a server
• Join a server
Thursday, May 24, 12
20. We’ll talk about
• TCP/IP
• Apple Bonjour, uPNP
• Socket, Stream, Buffers
• Create and publish a server
• Join a server
• Network messages
Thursday, May 24, 12
21. We’ll talk about
• TCP/IP
• Apple Bonjour, uPNP
• Socket, Stream, Buffers
• Create and publish a server
• Join a server
• Network messages
• Other...
Thursday, May 24, 12
23. We’ll see
• A screencast native application
Thursday, May 24, 12
24. We’ll see
• A screencast native application
• Something like chats
Thursday, May 24, 12
25. We’ll see
• A screencast native application
• Something like chats
• Where server can send images and other things
Thursday, May 24, 12
26. We’ll see
• A screencast native application
• Something like chats
• Where server can send images and other things
Follow the talk from your iPad using the same app
that we’re going to make
iCast
Thursday, May 24, 12
32. “Once upon a time”
Apple Talk
Thursday, May 24, 12
33. “Once upon a time”
Apple Talk
• Protocollo usato nelle reti Macintosh
Thursday, May 24, 12
34. “Once upon a time”
Apple Talk
• Protocollo usato nelle reti Macintosh
• Condivisione di file e stampanti
Thursday, May 24, 12
35. “Once upon a time”
Apple Talk
• Protocollo usato nelle reti Macintosh
• Condivisione di file e stampanti
• Uno dei primi sistemi plug-n-play
Thursday, May 24, 12
39. “things changes”
TCP/IP
• Negli anni novanta, con la diffusione del TCP/IP
Apple dismesse AppleTalk
Thursday, May 24, 12
40. “things changes”
TCP/IP
• Negli anni novanta, con la diffusione del TCP/IP
Apple dismesse AppleTalk
• Finalmente i Mac potevano connettersi con
altri sistemi operativi
Thursday, May 24, 12
46. Bonjour
L’implementazione Apple
di “Zero configuration network”
Thursday, May 24, 12
47. Bonjour
L’implementazione Apple
di “Zero configuration network”
“It just works”
Thursday, May 24, 12
48. Bonjour
L’implementazione Apple
di “Zero configuration network”
“It just works”
• mDNS + DNS Service discovery
Thursday, May 24, 12
49. Bonjour
L’implementazione Apple
di “Zero configuration network”
“It just works”
• mDNS + DNS Service discovery
• Address assignment
Thursday, May 24, 12
50. Bonjour
L’implementazione Apple
di “Zero configuration network”
“It just works”
• mDNS + DNS Service discovery
• Address assignment
• Name resolution
Thursday, May 24, 12
52. Bonjour
• Built-in con OSX e iOS.
Thursday, May 24, 12
53. Bonjour
• Built-in con OSX e iOS.
• Installabile su Windows
Thursday, May 24, 12
54. Bonjour
• Built-in con OSX e iOS.
• Installabile su Windows
• Incluso in Safari e iTunes.
Thursday, May 24, 12
55. Bonjour
• Built-in con OSX e iOS.
• Installabile su Windows
• Incluso in Safari e iTunes.
• Incluso nella Creative Suite 3 di Adobe
Thursday, May 24, 12
71. uPNP
Universal plug-n-play
Thursday, May 24, 12
72. uPNP
Universal plug-n-play
• It’s just another implementation of Zeroconf
Thursday, May 24, 12
73. uPNP
Universal plug-n-play
• It’s just another implementation of Zeroconf
• Media servers, Consoles, DLNA servers...
Thursday, May 24, 12
74. uPNP
Universal plug-n-play
• It’s just another implementation of Zeroconf
• Media servers, Consoles, DLNA servers...
• No authentication
Thursday, May 24, 12
75. uPNP
Universal plug-n-play
• It’s just another implementation of Zeroconf
• Media servers, Consoles, DLNA servers...
• No authentication
• No mDNS
Thursday, May 24, 12
90. Sockets vs Streams
• Socket: unique communication
endpoint on the network
Thursday, May 24, 12
91. Sockets vs Streams
• Socket: unique communication
endpoint on the network
• Stream: one-way channel through
which data is transmitted serially
Thursday, May 24, 12
94. We will use
• Stream sockets that are paired up
to allow our app to send data over
network using TCP/IP
Thursday, May 24, 12
95. We will use
• Stream sockets that are paired up
to allow our app to send data over
network using TCP/IP
• Asynchronous, non-blocking
operations
Thursday, May 24, 12
100. 3 Networking Classes
Server class:
• Creates a server
• Announces the server via Bonjour
Thursday, May 24, 12
101. 3 Networking Classes
Server class:
• Creates a server
• Announces the server via Bonjour
Connection class:
Thursday, May 24, 12
102. 3 Networking Classes
Server class:
• Creates a server
• Announces the server via Bonjour
Connection class:
• Resolves Bonjour Services
Thursday, May 24, 12
103. 3 Networking Classes
Server class:
• Creates a server
• Announces the server via Bonjour
Connection class:
• Resolves Bonjour Services
• Establishes connections to other servers
Thursday, May 24, 12
104. 3 Networking Classes
Server class:
• Creates a server
• Announces the server via Bonjour
Connection class:
• Resolves Bonjour Services
• Establishes connections to other servers
• Exchanges data via socket streams
Thursday, May 24, 12
105. 3 Networking Classes
Server class:
• Creates a server
• Announces the server via Bonjour
Connection class:
• Resolves Bonjour Services
• Establishes connections to other servers
• Exchanges data via socket streams
Server Browser class:
Thursday, May 24, 12
106. 3 Networking Classes
Server class:
• Creates a server
• Announces the server via Bonjour
Connection class:
• Resolves Bonjour Services
• Establishes connections to other servers
• Exchanges data via socket streams
Server Browser class:
• Browses for other servers via Bonjour
Thursday, May 24, 12
117. Network messages
How do we separate network messages from one another?
Thursday, May 24, 12
118. Network messages
How do we separate network messages from one another?
3 different ways:
Thursday, May 24, 12
119. Network messages
How do we separate network messages from one another?
3 different ways:
1) Make all messages have the same length
Thursday, May 24, 12
120. Network messages
How do we separate network messages from one another?
3 different ways:
1) Make all messages have the same length
2) Append a marker to each message
Thursday, May 24, 12
121. Network messages
How do we separate network messages from one another?
3 different ways:
1) Make all messages have the same length
2) Append a marker to each message
3) Send some kind of header with some info about how long the
message is
Thursday, May 24, 12
122. Network messages
How do we separate network messages from one another?
3 different ways:
1) Make all messages have the same length
2) Append a marker to each message
3) Send some kind of header with some info about how long the
message is
Thursday, May 24, 12
124. 3) Send some kind of header...
Thursday, May 24, 12
125. 3) Send some kind of header...
Writing messages
Thursday, May 24, 12
126. 3) Send some kind of header...
Writing messages
• Convert an object into a byte array and measure its length
Thursday, May 24, 12
127. 3) Send some kind of header...
Writing messages
• Convert an object into a byte array and measure its length
• Write the integer (4 bytes) that represents the length of our byte
array to the stream
Thursday, May 24, 12
128. 3) Send some kind of header...
Writing messages
• Convert an object into a byte array and measure its length
• Write the integer (4 bytes) that represents the length of our byte
array to the stream
• Write the actual byte array to the stream
Thursday, May 24, 12
130. Reading messages
• Read first 4 bytes and interpret those as an integer called “x”
Thursday, May 24, 12
131. Reading messages
• Read first 4 bytes and interpret those as an integer called “x”
• Read “x” number of bytes.
(stop when enough data has been received)
Thursday, May 24, 12
132. Reading messages
• Read first 4 bytes and interpret those as an integer called “x”
• Read “x” number of bytes.
(stop when enough data has been received)
• Turn received sequence of bytes back into an object.
Thursday, May 24, 12
143. Creating a server
We first create a stream
socket that will listen for
connections
Thursday, May 24, 12
144. Creating a server
We first create a stream
socket that will listen for
connections
CFSocketRef listeningSocket;
Thursday, May 24, 12
145. Creating a server
We first create a stream
socket that will listen for
connections
CFSocketRef listeningSocket;
using port number 0
Thursday, May 24, 12
146. Creating a server
We first create a stream
socket that will listen for
connections
CFSocketRef listeningSocket;
using port number 0
zero???? O_o
Thursday, May 24, 12
181. Recap
• Apple Talk, TCP/IP
• Bonjour flow cycle, uPNP
• Application layers, Socket, Stream, Buffers
• Write/Read Network messages
• Create and publish a server
Thursday, May 24, 12
182. Recap
• Apple Talk, TCP/IP
• Bonjour flow cycle, uPNP
• Application layers, Socket, Stream, Buffers
• Write/Read Network messages
• Create and publish a server
• Join a server
Thursday, May 24, 12
183. Recap
• Apple Talk, TCP/IP
• Bonjour flow cycle, uPNP
• Application layers, Socket, Stream, Buffers
• Write/Read Network messages
• Create and publish a server
• Join a server
• Interaction between the client and the server
Thursday, May 24, 12
184. Recap
• Apple Talk, TCP/IP
• Bonjour flow cycle, uPNP
• Application layers, Socket, Stream, Buffers
• Write/Read Network messages
• Create and publish a server
• Join a server
• Interaction between the client and the server
• One more thing: server broadcasts messages to connected clients
Thursday, May 24, 12