开始学nodejs——net模块
net模块的组成部分
net.Server类
net.Socket类
事件
方法
属性
工厂方法
练习代码
netServer
var net=require("net"); /**
* 创建server
*/
function t1(){
var server=net.createServer(function(socket){
//socket.end("good bye!");
socket.write("hello,i'm server!");
console.log("client connected! %j:%j",socket.remoteAddress,socket.remotePort);
socket.on("data",function(data){
console.log("recived from client:",data.toString());
})
socket.on("close",function(had_error){
if(!had_error){
console.log("client closed success! %j:%j",socket.remoteAddress,socket.remotePort);
}
else{
console.log("client close error! %j:%j",socket.remoteAddress,socket.remotePort);
}
})
socket.on("error",function(err){
console.log("!!!err!!!",err);
});
//setTimeout(function(){
// socket.end("我结束了","utf8");
//},3000);
});
server.listen({
port:8889
},function(){
var address=server.address();
console.log(" opened server on address %j ",address);
}); var a=1;
}
t1();
netSocket
var net = require("net"); /**
* 初始化net clientSocket对象,测试发送数据,关闭连接
*/
function t1(autoclose){
var inteval;
var socket1=new net.Socket({
readable:true,
writable:true,
allowHalfOpen:true
});
socket1.on("data",function(data){
console.log("recived from server:"+data.toString());
});
socket1.on("close",function(){
console.log(" client closed success ");
});
socket1.on("error",function(err){
console.log(" client error: ",err);
});
socket1.connect({
host:"localhost",
port:8889
},function(){
console.log(" server connected");
inteval=setInterval(function(){
socket1.write("hello,i'm client!"+Math.random())
},3000)
});
if(autoclose){
setTimeout(function(){
socket1.destroy();
clearInterval(inteval)
//socket1.end("我结束了","utf8");
},3000);
}
} /**
* 通过net.connect创建clientSocket对象
*/
function t2(){
var client = net.connect({port: 8889},
function() { //'connect' listener
console.log('connected to server!!!');
client.write('world!\r\n');
});
client.on('data', function(data) {
console.log(data.toString());
//client.end();
});
client.on('end', function() {
console.log('disconnected from server');
}); } t1(false);
t1(true);
//t2();
//for(var i=0;i<1000;i++){
// console.log("连接个数:",i+1);
// t1(false);
//}
下图为server控制台的截图
开始学nodejs——net模块的更多相关文章
- 开始学nodejs —— 调试篇
新学习一种技术,肯定会遇到很多坑,我们需要找到这些坑,弄清楚这些坑出现的原因和其中的原理.这种操作就叫做调试. 程序调试的方法和工具多种多样,在这里我总结一下我在学习nodejs的过程中,学到的和用到 ...
- nodejs事件模块
nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...
- 配置 Windows 下的 nodejs C++ 模块编译环境
根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境". 所有需要的安装文件, 我都下载好放到百度云盘了: nod ...
- NodeJS http 模块
#4 NodeJS http 模块 工作目录 server.js var http = require('http'); var fs = require('fs'); var path = requ ...
- nodejs的模块系统(实例分析exprots和module.exprots)
前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...
- nodejs cluster模块初探
大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,为了使用多核cpu来提高性能 就有了cluster,让node可以利用多核CPU实现并行 ...
- NodeJS Web模块
NodeJS Web模块 本文介绍nodeJS的http模块的基本用法,实现简单服务器和客户端 经典Web架构 Client:客户端一般指浏览器,通过HTTP协议向服务器发送请求(request) S ...
- 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp
配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...
- nodejs 视频教程《一起学nodejs》
一起学nodejs 讲师: matthew vscode+nodejs4.6 http://list.youku.com/albumlist/show/id_27966955.html?spm=a ...
随机推荐
- Jenkins 安装的HTML Publisher Plugin 插件无法展示ant生成的JunitReport报告
最近在做基于jenkins ant junit 的测试持续集成,单独ant junit生成的junitreport报告打开正常,使用Jenkins的HTML Publisher Plugin 插件无 ...
- IE6/7下空div占用空间的问题
最近注意力没在前端上面,工作碰到这样一个问题,下意识的写了句 font-size:0;line-height:0;哪知道引发了更大的bug.后来插入数据进去的时候都不显示了..再后来百度一番找到,原来 ...
- 探索ASP.NET MVC5系列之~~~3.视图篇(下)---包含常用表单和暴力解猜防御
其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...
- [C#] 了解过入口函数 Main() 吗?带你用批处理玩转 Main 函数
了解过入口函数 Main() 吗?带你用批处理玩转 Main 函数 目录 简介 特点 方法的参数 方法的返回值 与批处理交互的一个示例 简介 我们知道,新建一个控制台应用程序的时候,IDE 会同时创建 ...
- 深入研究Visual studio 2017 RC新特性
在[Xamarin+Prism开发详解三:Visual studio 2017 RC初体验]中分享了Visual studio 2017RC的大致情况,同时也发现大家对新的Visual Studio很 ...
- 基于RN开发的一款视频配音APP(开源)
在如今React.ng.vue三分天下的格局下,不得不让自己加快学习的脚步.虽然经常会陷入各种迷茫,学得越多会发现不会的东西也被无限放大,不过能用新的技术作出一些小项目小Demo还是会给自己些许自信与 ...
- 微信小程序前端源码逻辑和工作流
看完微信小程序的前端代码真的让我热血沸腾啊,代码逻辑和设计一目了然,没有多余的东西,真的是大道至简. 废话不多说,直接分析前端代码.个人观点,难免有疏漏,仅供参考. 文件基本结构: 先看入口app.j ...
- Log4net - 项目使用的一个简单Demo
参考页面: http://www.yuanjiaocheng.net/entity/entitytypes.html http://www.yuanjiaocheng.net/entity/entit ...
- 【微信小程序开发】之如何获取免费ssl证书【图文步骤】
微信小程序要求所有网络请求都走ssl加密,因此我们开发服务端接口需要配置为https 这篇文章介绍一下如何 在 startssl 申请一个免费的ca证书. 1. 打开网站 https://www.s ...
- linux系统oracle-ora12505问题解决方案一
说明:(1)Linux版本 Linux version 2.6.32.12-0.7-default (geeko@buildhost) (gcc version 4.3.4 [gcc-4_3-bran ...