~~ 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的更多相关文章

  1. nodeschool.io 4

    ~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...

  2. nodeschool.io 3

    ~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...

  3. nodeschool.io 2

    ~~  BABY STEPS  ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...

  4. nodeschool.io 9

    ~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...

  5. nodeschool.io 8

    ~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...

  6. nodeschool.io 7

    ~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...

  7. nodeschool.io 6

    ~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...

  8. nodeschool.io 5

    ~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...

  9. npm install socket.io遇到的问题

    解决方法: 输入 npm install socket.io 前,先执行下面 npm config set proxy "http://yourip:port" 生产的npm-de ...

随机推荐

  1. .Net六大验证及使用方法

    C#包含有六种验证方式,分别为: 一.非空验证  RequiredFieldValidator. 二.对比验证 CompareValidator. 三.范围验证 RangeValidator. 四.正 ...

  2. BZOJ 1835 基站选址(线段树优化DP)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1835 题意:有N个村庄坐落在一条直线上,第 i(i>1)个村庄距离第1个村庄的距离 ...

  3. DrawIndexedPrimitive参数详解

    HRESULT DrawIndexedPrimitive( [in]  D3DPRIMITIVETYPE Type, [in]  INT BaseVertexIndex, [in]  UINT Min ...

  4. Windows控制台程序“选定模式”的问题

    最近用Nodejs写了个代理程序,一直用的好好的,木有问题,今天突然发现不能用了,使用telnet去连代理的端口也能连通,可是服务就是不能正常使用,提示连接超时. 当时猜测是Nodejs的某个地方阻塞 ...

  5. Django 分页2 (Pagination)

    分页是Web应用常用的手法,Django提供了一个分页器类Paginator(django.core.paginator.Paginator),可以很容易的实现分页的功能.该类有两个构造参数,一个是数 ...

  6. 不小心删除数据--利用MySQL的binlog恢复数据

    MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...

  7. FZU 2213 Common Tangents(公切线)

    Description 题目描述 Two different circles can have at most four common tangents. The picture below is a ...

  8. iOS - UIDevice

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIDevice : NSObject @available(iOS 2.0, *) public class UI ...

  9. IO端口、IO内存、IO空间、内存空间的含义和联系

    1,IO空间:X86一个特有的空间,与内存空间独立的空间,同样利用IO空间可以操作数据,只不过是利用对应的IO端口操作函数,例如inb(), inbw(), inl(); outb(), outw() ...

  10. Python的lambda表达式

    使用lambda来创建匿名函数,而用def创建的方法是有名称的,除了从表面上的方法名不一样外,python lambda还有哪些和def不一样呢? 1 python lambda会创建一个函数对象,但 ...