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 ...
随机推荐
- UVA 1292 十二 Strategic game
Strategic game Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- 对 strcpy_s 若干测试
今天发现如果strcpy这函数,目标buffer太小,会有意想不到的崩溃.而且不容易调试.以后尽量要用strcpy_s了. strcpy_s是strcpy的更安全的版本 1.当目标字符串参数是一个字符 ...
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载JDK(Java Development Kit,Java开发包,Java开发工具)是一个写Java的applet和 ...
- 业务对象和BAPI
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- python_way day17 html-day3 前端插件(fontawsome,easyui,bootstrap,jqueryui,bxslider,jquerylazyload),web框架
python_way day17 一.模板插件 图标的插件 fontawsome: 后台管理: easyui jqueryui 很多网站都会用: bootstrap :引入jQuery:(2.x,1. ...
- CSS3 transition/transform
Transition 1.简写属性transition,可以包括四个属性,这四个属性的顺序按照下面介绍的顺序书写,即transition:property duration timing-functi ...
- varchar(10)与nvarchar(10)有什么区别
前者是非unicode型,存储字符按1个算(内部空间存储占1字节),存储汉字的话按2个算, 就是可以存10个字符或者5个汉字 后者是unicode型,存储什么都是按1个算(内部空间存储占2字节), 就 ...
- iOS - UITabBarController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate ...
- [转载] HTTP请求的TCP瓶颈分析
原文: http://bhsc881114.github.io/2015/06/23/HTTP%E8%AF%B7%E6%B1%82%E7%9A%84TCP%E7%93%B6%E9%A2%88%E5%8 ...
- Android Contacts (android通讯录读取)-content provider
Content Provider 在数据处理中,Android通常使用Content Provider的方式.Content Provider使用Uri实例作为句柄的数据封装的,很方便地访问地进行数据 ...