~~ 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. SqlSever基础 datalength函数 计算前后都有空格的字符串的长度

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  2. CodeForces 567C Geometric Progression

    Geometric Progression Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  3. jquery 之height(),innerHeight(),outerHeight()方法区别详解

    在jQuery中,获取元素高度的函数有3个,它们分别是height(). innerHeight().outerHeight(). 与此相对应的是,获取元素宽度的函数也有3个,它们分别是width() ...

  4. ABAP DESCRIBE语句

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. C#:常规属性和自动实现的属性

    根据属性的实现方式,属性可分为自动实现的属性和常规属性. 常规属性需要具体的人为的实现get访问器或者set访问器,而且一般需要有一个字段与之相对应:而自动实现的属性的get和set访问器的实现部分被 ...

  6. python介绍(转载)

    Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  7. run a Freight robot (2)

    3.  Network Setup Connecting Freight to a Monitor The easiest way to configure the wireless networki ...

  8. 个人博客作业WEEK 1

    一.项目时间规划与实际用时 PSP2.1 Personal Software Process Stages 预计时间/h 实际时间/h Planning 计划   · Estimate · 估计这个任 ...

  9. iOS - 3DTouch 3D 触摸

    1.3DTouch 简介 3DTouch 是 iOS9 + 系统下,在 iPhone6s(iPhone6s Plus)+ 手机上才能够使用的功能. 1.1 3DTouch 基本类型 1.主屏幕快速选项 ...

  10. IO端口和IO内存的区别及分别使用的函数接口

    每个外设都是通过读写其寄存器来控制的.外设寄存器也称为I/O端口,通常包括:控制寄存器.状态寄存器和数据寄存器三大类.根据访问外设寄存器的不同方式,可以把CPU分成两大类.一类CPU(如M68K,Po ...