Chinaunix首页 | 论坛 | 博客
  • 博客访问: 652807
  • 博文数量: 26
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 14:29
个人简介

7年游戏服务器开发,擅长c/c++,javesript,php;熟悉linux,mysql/redis,elasticsearch;开源爱好者.github : https://github.com/yuyunliuhen

文章分类

全部博文(26)

文章存档

2016年(1)

2015年(3)

2014年(3)

2013年(19)

分类: JavaScript

2013-07-19 08:17:24


点击(此处)折叠或打开

  1. // tcp_server.js
  2. var net = require("net");
  3.  
  4. var HOST = "127.0.0.1"
  5. var PORT = 9876
  6. var BUFSIZE = 256
  7. var buf = new Buffer(BUFSIZE);
  8.  
  9. // connection 's event prototype is function(sock), you can also use net.createServer( function(sock){ ... } );
  10. // the funcetion pass to net.createServer becomes the event handler for 'connection' event;
  11. // the sock object the call back function receive unique for each connection.
  12. function onConnection(sock)
  13. {
  14.     console.log("connected :" + sock.remoteAddress + " " + sock.remotePort);
  15.     // add a 'data' event handler to this instance of socker
  16.     sock.on("data",function(buf)
  17.     {
  18.         console.log("data from clinet: " + sock.remoteAddress + " : " + buf.toString('utf8', 0, buf.length));
  19.         // write back data to the connection of socket
  20.         sock.write(buf.toString('utf8', 0, buf.length));
  21.     });
  22.     // add a 'close' event handler to this instance of socker
  23.     sock.on("close",function(data)
  24.     {
  25.         console.log("closed " + sock.remoteAddress + " " + sock.remotePort);
  26.     });
  27. }
  28.  
  29. // create a server instance, you also can use another way,
  30. /*
  31.     var server = net.createServer();
  32.     server.listen(PORT,HOST);
  33.     server.on('connection',function(sock){...} );
  34. */
  35. var server = net.createServer( false,onConnection );
  36. // listen at PORT HOST
  37. server.listen(PORT,HOST);
  38.  
  39. console.log("server listen on " + HOST + " : " + PORT);

点击(此处)折叠或打开

  1. // tcp_client.js
  2. var net = require("net");
  3.  
  4. var HOST = "127.0.0.1"
  5. var PORT = 9876
  6. var BUFSIZE = 256
  7. var buf = new Buffer(BUFSIZE);
  8. var client = new net.Socket();
  9. // open a connection for a gived socket
  10. client.connect(PORT,HOST,function()
  11. {
  12.     console.log("connect to :" + HOST + " : " + PORT);
  13.     // write data to server
  14.     var len = buf.write("0123456789");
  15.     console.log("data :" + buf.toString('utf8', 0, len));
  16.     client.write(buf);
  17. });
  18. // add a 'data' event handler for the client socket,the callback funtion receive the data whick server give back
  19. client.on("data",function(buf)
  20. {
  21.     console.log("data :" + buf.toString('utf8', 0, buf.length));
  22.     client.write(buf);
  23. });
  24.  
  25. // add a 'close' event handler for the client socket
  26. client.on("close",function()
  27. {
  28.     console.log("connect closed!");
  29. });

参考:
         Node.js v0.10.12 Manual & Documentation
         

         node 入门
         
        
         TCP Scoket programming in Node.js
         

 


    

阅读(7166) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~