Skip to content

Commit a96c159

Browse files
committed
Add "Networking" chapter and server/client recipes.
1 parent 1ee6471 commit a96c159

File tree

7 files changed

+234
-0
lines changed

7 files changed

+234
-0
lines changed

chapters/index.textile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ chapters:
1313
- jQuery
1414
- Regular Expressions
1515
- AJAX
16+
- Networking
1617
- Design Patterns
1718
---
1819

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
layout: recipe
3+
title: Basic Client
4+
chapter: Networking
5+
---
6+
7+
h2. Problem
8+
9+
You want to access a service provided over the network.
10+
11+
12+
h2. Solution
13+
14+
Create a basic TCP client.
15+
16+
h3. Node.js
17+
18+
{% highlight coffeescript %}
19+
net = require 'net'
20+
21+
domain = 'localhost'
22+
port = 9001
23+
24+
connection = net.createConnection port, domain
25+
26+
connection.on 'connect', () ->
27+
console.log "Opened connection to #{domain}:#{port}."
28+
29+
connection.on 'data', (data) ->
30+
console.log "Received: #{data}"
31+
connection.end()
32+
{% endhighlight %}
33+
34+
h3. Example Usage
35+
36+
Accessing the <a href="/chapters/networking/basic-server.html">Basic Server</a>:
37+
38+
*$ coffee basic-client.coffee*
39+
Opened connection to localhost:9001
40+
Received: Hello, World!
41+
42+
h2. Discussion
43+
44+
See also the <a href="/chapters/networking/basic-server.html">Basic Server</a>, the <a href="/chapters/networking/bi-directional-client.html">Bi-Directional Client</a>, and the <a href="/chapters/networking/bi-directional-server.html">Bi-Directional Server recipes</a>.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
layout: recipe
3+
title: Basic Server
4+
chapter: Networking
5+
---
6+
7+
h2. Problem
8+
9+
You want to provide a service over a network.
10+
11+
12+
h2. Solution
13+
14+
Create a basic TCP server.
15+
16+
h3. Node.js
17+
18+
{% highlight coffeescript %}
19+
net = require 'net'
20+
21+
domain = 'localhost'
22+
port = 9001
23+
24+
server = net.createServer (socket) ->
25+
console.log "Received connection from #{socket.remoteAddress}"
26+
socket.write "Hello, World!\n"
27+
socket.end()
28+
29+
console.log "Listening to #{domain}:#{port}"
30+
server.listen port, domain
31+
{% endhighlight %}
32+
33+
h3. Example Usage
34+
35+
Accessed by the <a href="/chapters/networking/basic-client.html">Basic Client</a>:
36+
37+
*$ coffee basic-server.coffee*
38+
Listening to localhost:9001
39+
Received connection from 127.0.0.1
40+
Received connection from 127.0.0.1
41+
[...]
42+
43+
h2. Discussion
44+
45+
See also the <a href="/chapters/networking/basic-client.html">Basic Client</a>, the <a href="/chapters/networking/bi-directional-server.html">Bi-Directional Server</a>, and the <a href="/chapters/networking/bi-directional-client.html">Bi-Directional Client recipes</a>.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
layout: recipe
3+
title: Bi-Directional Client
4+
chapter: Networking
5+
---
6+
7+
h2. Problem
8+
9+
You want to access a service that provides a persistent connection over the network.
10+
11+
12+
h2. Solution
13+
14+
Create a bi-directional TCP client.
15+
16+
h3. Node.js
17+
18+
{% highlight coffeescript %}
19+
net = require 'net'
20+
21+
domain = 'localhost'
22+
port = 9001
23+
24+
ping = (socket, delay) ->
25+
console.log "Pinging server"
26+
socket.write "Ping"
27+
nextPing = -> ping(socket, delay)
28+
setTimeout nextPing, delay
29+
30+
connection = net.createConnection port, domain
31+
32+
connection.on 'connect', () ->
33+
console.log "Opened connection to #{domain}:#{port}"
34+
ping connection, 2000
35+
36+
connection.on 'data', (data) ->
37+
console.log "Received: #{data}"
38+
39+
connection.on 'end', (data) ->
40+
console.log "Connection closed"
41+
process.exit()
42+
{% endhighlight %}
43+
44+
h3. Example Usage
45+
46+
Accessing the <a href="/chapters/networking/bi-directional-server.html">Bi-Directional Server</a>:
47+
48+
*$ coffee bi-directional-client.coffee*
49+
Opened connection to localhost:9001
50+
Pinging server
51+
Received: You have 0 peers on this server
52+
Pinging server
53+
Received: You have 0 peers on this server
54+
Pinging server
55+
Received: You have 0 peers on this server
56+
[...]
57+
Connection closed
58+
59+
h2. Discussion
60+
61+
This particular example initiates contact with the server and starts the conversation in the _on 'connect'_ handler. The bulk of the work in a real client, however, will lie in the _on 'data'_ handler, which processes output from the server.
62+
63+
See also the <a href="/chapters/networking/bi-directional-server.html">Bi-Directional Server</a>, the <a href="/chapters/networking/basic-client.html">Basic Client</a>, and the <a href="/chapters/networking/basic-server.html">Basic Server</a> recipes.
64+
65+
h3. Exercises
66+
* Add support for choosing the target domain and port based on command-line arguments or from a configuration file.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
layout: recipe
3+
title: Bi-Directional Server
4+
chapter: Networking
5+
---
6+
7+
h2. Problem
8+
9+
You want to provide a service over a network while maintaining a connection with clients.
10+
11+
12+
h2. Solution
13+
14+
Create a bi-directional TCP server.
15+
16+
h3. Node.js
17+
18+
{% highlight coffeescript %}
19+
net = require 'net'
20+
21+
domain = 'localhost'
22+
port = 9001
23+
24+
server = net.createServer (socket) ->
25+
console.log "New connection from #{socket.remoteAddress}"
26+
27+
socket.on 'data', (data) ->
28+
console.log "#{socket.remoteAddress} sent: #{data}"
29+
others = server.connections - 1
30+
socket.write "You have #{others} #{others == 1 and "peer" or "peers"} on this server"
31+
32+
console.log "Listening to #{domain}:#{port}"
33+
server.listen port, domain
34+
{% endhighlight %}
35+
36+
h3. Example Usage
37+
38+
Accessed by the <a href="/chapters/networking/bi-directional-client.html">Bi-Directional Client</a>:
39+
40+
*$ coffee bi-directional-server.coffee*
41+
Listening to localhost:9001
42+
New connection from 127.0.0.1
43+
127.0.0.1 sent: Ping
44+
127.0.0.1 sent: Ping
45+
127.0.0.1 sent: Ping
46+
[...]
47+
48+
h2. Discussion
49+
50+
The bulk of the work lies in the _on 'data'_ handler, which processes all of the input from the client.
51+
52+
See also the <a href="/chapters/networking/bi-directional-client.html">Bi-Directional Client</a>, the <a href="/chapters/networking/basic-client.html">Basic Client</a>, and the <a href="/chapters/networking/basic-server.html">Basic Server</a> recipes.
53+
54+
h3. Exercises
55+
* Add support for choosing the target domain and port based on command-line arguments or from a configuration file.
56+

chapters/networking/index.textile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
layout: chapter
3+
title: Networking
4+
chapter: Networking
5+
---
6+
7+
{% capture url %}/chapters/{{ page.chapter | replace: ' ', '_' | downcase }}{% endcapture %}
8+
{% capture indexurl %}{{ url }}/index.html{% endcapture %}
9+
10+
{% for page in site.pages %}
11+
{% if page.url contains url %}
12+
{% unless page.url == indexurl %}
13+
* <a href="{{ page.url | replace: '.html', '' }}">{{ page.title }}</a>
14+
{% endunless %}
15+
{% endif %}
16+
{% endfor %}
17+

wanted-recipes.textile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ h2. Regular Expressions
114114
* Replacing substrings # "foo bar baz".replace( /ba./, 'foo') # => "foo foo baz"
115115
* Replace HTML tags with named HTML entities # <br/> => &lt;br/&gt;
116116

117+
h2. Networking
118+
119+
* Basic HTTP server
120+
* Basic HTTP client
121+
117122
h2. AJAX
118123

119124
* Getting data from a remote server # using raw XHTTPRequest instead of jQuery's $.ajax

0 commit comments

Comments
 (0)