nodejs学习:net模块

官方API文档地址:https://nodejs.org/api/net.html

  1. 创建一个server.js
     1 var net = require('net');
     2 var PORT  = 8099;
     3 var HOST = '127.0.0.1'
     4 
     5 var server = net.createServer();
     6 server.on('connection', function(client) {
     7     client.name = client.remoteAddress + ':' + client.remotePort
     8     client.write('Hi ' + client.name + '!\n');
     9 
    10     client.on('data', function(data) {
    11         client.write(data);
    12     })
    13     client.on('end', function() {
    14         client.write('Bye ' + client.name + '!\n');
    15     })
    16 
    17     client.on('error', function(e) {
    18         console.log('error:'+ e.message);
    19     })
    20 });
    21 
    22 server.on('listening', function() {
    23     console.log('Server is listening on port', PORT);
    24 });
    25 server.listen(PORT,HOST);

     

  2. 连接
posted @ 2015-08-20 18:02  Frida-  阅读(642)  评论(0编辑  收藏  举报