nodeschool.io 10
2013-11-20 19:10
295 查看
~~ TIME SERVER ~~
Write a TCP time server!
Your server should listen to TCP connections on port 8000. For each
connection you must write the current date & time in the format:
YYYY-MM-DD hh:mm
followed by a newline character. Month, day, hour and minute must be
zero-filled to 2 integers. For example:
2013-07-06 07:42
----------------------------------------------------------------------
HINTS:
For this exercise we'll be creating a raw TCP server. There's no HTTP
involved here so we need to use the `net` module from Node core which
has all the basic networking functions.
The `net` module has a method named `net.createServer()` that takes a
callback function. Unlike most callbacks in Node, the callback used by
`createServer()` is called more than once. Every connection received
by your server triggers another call to the callback. The callback
function has the signature:
function (socket) { ... }
`net.createServer()` also returns an instance of your `server`. You
must call `server.listen(portNumber)` to start listening on a
particular port.
A typical Node TCP server looks like this:
var net = require('net')
var server = net.createServer(function (socket) {
// socket handling logic
})
server.listen(8000)
The `socket` object contains a lot of meta-data regarding the
connection, but it is also a Node duplex Stream, in that it can be
both read from, and written to. For this exercise we only need to
write data and then close the socket.
Use `socket.write(data)` to write data to the socket and
`socket.end()` to close the socket. Alternatively, the `.end()` method
also takes a data object so you can simplify to just:
`socket.end(data)`.
Documentation on the `net` module can be found by pointing your
browser here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\net.html
To create the date, you'll need to create a custom format from a
`new Date()` object. The methods that will be useful are:
date.getFullYear()
date.getMonth() (starts at 0)
date.getDate() (returns the day of month)
date.getHours()
date.getMinutes()
Or, if you want to be adventurous, use the `moment` package from npm.
Details of this excellent time/date handling library can be found
here: http://momentjs.com/docs/
----------------------------------------------------------------------
timeServer.js
Write a TCP time server!
Your server should listen to TCP connections on port 8000. For each
connection you must write the current date & time in the format:
YYYY-MM-DD hh:mm
followed by a newline character. Month, day, hour and minute must be
zero-filled to 2 integers. For example:
2013-07-06 07:42
----------------------------------------------------------------------
HINTS:
For this exercise we'll be creating a raw TCP server. There's no HTTP
involved here so we need to use the `net` module from Node core which
has all the basic networking functions.
The `net` module has a method named `net.createServer()` that takes a
callback function. Unlike most callbacks in Node, the callback used by
`createServer()` is called more than once. Every connection received
by your server triggers another call to the callback. The callback
function has the signature:
function (socket) { ... }
`net.createServer()` also returns an instance of your `server`. You
must call `server.listen(portNumber)` to start listening on a
particular port.
A typical Node TCP server looks like this:
var net = require('net')
var server = net.createServer(function (socket) {
// socket handling logic
})
server.listen(8000)
The `socket` object contains a lot of meta-data regarding the
connection, but it is also a Node duplex Stream, in that it can be
both read from, and written to. For this exercise we only need to
write data and then close the socket.
Use `socket.write(data)` to write data to the socket and
`socket.end()` to close the socket. Alternatively, the `.end()` method
also takes a data object so you can simplify to just:
`socket.end(data)`.
Documentation on the `net` module can be found by pointing your
browser here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\net.html
To create the date, you'll need to create a custom format from a
`new Date()` object. The methods that will be useful are:
date.getFullYear()
date.getMonth() (starts at 0)
date.getDate() (returns the day of month)
date.getHours()
date.getMinutes()
Or, if you want to be adventurous, use the `moment` package from npm.
Details of this excellent time/date handling library can be found
here: http://momentjs.com/docs/
----------------------------------------------------------------------
timeServer.js
var net = require('net'); function zero(num){ num = (num > 10 ? "" : "0" )+ num; return num; } function now(){ var d = new Date(); return d.getFullYear()+"-"+zero(d.getMonth()+1) +"-"+ zero(d.getDate())+" "+zero(d.getHours())+":"+zero(d.getMinutes()); } var server = net.createServer(function(socket) { //'connection' listener socket.end(now() + "\n"); }); server.listen(8000);
相关文章推荐
- nodeschool.io 7
- Node(10) File System, IO, Stream
- nodeschool.io 5
- nodeschool.io 2
- nodeschool.io 6
- nodeschool.io 3
- nodeschool.io 9
- nodeschool.io 4
- Node+Express+MongoDB + Socket.io搭建实时聊天应用实战教程(三)--前后端环境配置
- 快速搭建 Node.js / io.js 开发环境以及加速 npm
- SQLite剖析(10):异步IO模式、共享缓存模式和解锁通知
- 单页WEB应用(七),WebServer Node.js 之 Socket.IO
- node.js中的socket.io的广播消息
- org.apache.hadoop.hdfs.server.datanode.DataNode: java.io.IOException: Incompatible namespaceIDs in /
- 使用Node.js+Socket.IO搭建WebSocket实时应用
- node+socket 即时聊天 掌握node socket.io基本流程
- 10 款 Node.js 框架,可用于你的下一个项目
- Windows 10 下安装 npm 后全局 node_modules 和 npm-cache 文件夹的设置
- NodeJS Socket.io
- Node.js socket.io前端后端通讯简单例子