node.js 下 tcp socket 编程-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 684157
  • 博文数量: 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
         

 


    

阅读(7404) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图