nodeschool.io 10
~~ 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
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 10的更多相关文章
- nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...
- nodeschool.io 3
~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...
- nodeschool.io 2
~~ BABY STEPS ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...
- nodeschool.io 9
~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...
- nodeschool.io 8
~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- nodeschool.io 6
~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...
- nodeschool.io 5
~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...
- npm install socket.io遇到的问题
解决方法: 输入 npm install socket.io 前,先执行下面 npm config set proxy "http://yourip:port" 生产的npm-de ...
随机推荐
- HTML框架与布局
原文:http://www.cnblogs.com/yyhh/p/4210659.html HTML块 HTML块元素 块元素在显示时,通常会以新行开始 如:<h1>.<p>. ...
- 9.Methods(二)
4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...
- NoSQL数据库:数据的一致性
NoSQL数据库:数据的一致性 读取一致性 强一致性 在任何时间访问集群中任一结点,得到的数据结果一致: 用户一致性 对同一用户,访问集群期间得到的数据一致: 解决用户一致性:使用粘性会话,将会话绑定 ...
- Installing Python 3.5.2 from source
Here are the procedures we are to follow, Download the source code of an official Python release. Co ...
- Codeforces Round #378 (Div. 2) C. Epidemic in Monstropolis 模拟
C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...
- ctDNA 相关网站-liquid-biopsy
http://www.gene-quantification.de/liquid-biopsy.html Liquid Biopsy -- Definitions Liquid Biopsy -- r ...
- 慎重别选择到"僵尸"软件
何谓僵尸? 没有灵魂,动作单一,我们电视电影上经常看见, 只能往前跳,不会走路, 手向前伸直,左右摆攻击. 何谓"僵尸"软件? 根据僵尸的特性,大概有如下几类: 1.没有任何创新性 ...
- server-pc--------------->lspci,lsusb,meminfo等配置信息
安装yum install pciutils usbutils [root@server09 ~]# [root@server09 ~]# lspci00:00.0 Host bridge: Inte ...
- ORACLE SQL 分组
select max(cost),suppliercode from tel_bill where period = '2014005' group by suppliercode;select * ...
- Android_相关路径
1. Android应用安装涉及到如下几个目录:system/app 系统自带的应用程序,无法删除.data/app 用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.da ...