使用nodejs的net模块创建TCP服务器
laiqun@msn.cn

Contents

1. 代码实现

const PORT = 18001;
const HOST = '127.0.0.1';
var net = require('net');
var clientHandler = function(socket){
console.log('someon connected');
socket.on('data',function dataHandler(data){
console.log(socket.remoteAddress,socket.remotePort,'send',data.toString());
socket.write('server received\n');
});////data 当客户端发来数据时触发
socket.on('close',function(){
console.log(socket.remoteAddress,soctet.remotePort,'disconnected');
//socket之后被销毁
});
}; var app= net.createServer(clientHandler);
app.listen(PORT,HOST);
console.log('tcp server running on tcp:://',HOST,':',PORT);
//当connect事件发生时,触发回调函数

2. 使用telnet连接服务器测试

telnet localhost 18001 会触发connect事件 输入一段字 回车,便可发送 使用netstat -antp|grep 18001

3. 创建一个TCP的client

var net = require('net');
const HOST = '127.0.0.1';
const PORT = 18001;
var tcpClient = net.SOcket();
tcpClient.connect(PORT,HOST,function(){
console.log('connect success.');
tcpClient.write('this is tcp client by nodejs');
});
tcpClient.on('data',function(data){//监听
console.log('received: ',data);
});

使用nodejs的net模块创建TCP服务器的更多相关文章

  1. 使用nodejs的http模块创建web服务器

    使用nodejs的http模块创建web服务器 laiqun@msn.cn Contents 1. web服务器基础知识 2. Node.js的Web 服务器 3. 代码实现 1. web服务器基础知 ...

  2. 使用net模块创建tcp服务器

    demo /** * Created by ZXW on 2018/4/6. */ var net=require('net'); ; const HOST='localhost'; var clie ...

  3. swoole 创建tcp服务器

    server.php <?php /** * 创建tcp服务器 * Date: 2019/1/15 */ $serv = new swoole_server('127.0.0.1', 9501) ...

  4. 【Swoole】简单安装与创建TCP服务器

    pecl install swoole PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了php语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据 ...

  5. node.js中通过dgram数据报模块创建UDP服务器和客户端

    node.js中 dgram 模块提供了udp数据包的socket实现,可以方便的创建udp服务器和客户端. 一.创建UDP服务器和客户端 服务端: const dgram = require('dg ...

  6. nodejs的express框架创建https服务器

    一 openssl创建https私钥和证书 1.下载windows版openssl: http://slproweb.com/products/Win32OpenSSL.html Win64OpenS ...

  7. nodejs 用http模块搭建的服务器的路由,以及路由代码的重构过程

    我们打开浏览器浏览网页时,点击上面不同的模块,地址栏中的路由会发生相应的变化,从而,浏览器向服务器发起请求的内容也会发生改变,那么服务端,是如何来做的呢? 服务端也是,通过路由来做出不同的响应的,我们 ...

  8. node.js使用express模块创建web服务器应用

    1.安装express模块 在命令行输入 npm install body-parser --save npm install express --save 2.创建app.js文件 /*应用程序入口 ...

  9. 创建TCP服务器和TCP客户端

    import sockethost='127.0.0.1'port=8080web=socket.socket()web.bind((host,port))web.listen(5)# 设置最多连接数 ...

随机推荐

  1. Spring Timer实现

    定时器:继承java.util.TimerTask类实现run方法 package com.zbb.framework.util.timer; import java.util.TimerTask; ...

  2. touchesBegan: withEvent: <--- with UIScrollView / UIImageView

    touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对 ...

  3. Sping3.0版本+Quartz完成定时任务

    ----------------------不使用注解在XML中配置完成定时任务---------------------- 1.需要导入的jar包 2.编写我们的定时任务的完成类 3.在Spring ...

  4. akka 文章 博客

    http://blog.csdn.net/wsscy2004/article/category/2430395 Actor生命周期理解 Actor生命周期理解 镇图:Actor内功心法图 Actor的 ...

  5. linux文件特殊属性介绍(s,s,t)

    文件的权限有rwx这3个读.写.执行的权限.但是,怎么 /tmp权限有些奇怪?还有, /usr/bin/passwd也有些奇怪,怎么回事呢? [root@linux ~]# ls -ld /tmp ; ...

  6. 一个Cmake的例子

    命令查询列表:http://www.cmake.org/cmake/help/v3.2/manual/cmake-commands.7.html # # Official dependency num ...

  7. 【超级干货】手机移动端WEB资源整合:转载

    转载于:http://www.w3cfuns.com/notes/24611/fbba9cbd616e795360ea45515494aa53.html meta基础知识 H5页面窗口自动调整到设备宽 ...

  8. dfs.replication 参数 动态修改

    首先 dfs.replication这个参数是个client参数,即node level参数.需要在每台datanode上设置.其实默认为3个副本已经够用了,设置太多也没什么用. 一个文件,上传到hd ...

  9. Linux设置静态IP【转】

    一只小码 2016-08-16 10:32 测试服务器OS: Centos 6.5 x64 本机OS: Ubuntu 14.04 x64 由于Virtualbox当时安装Centos 6.5的时候设置 ...

  10. grub修复

    sudo mount /dev/sda1 /boot/efi sudo modprobe efivarfs sudo grub-install /dev/sda sudo update-grub su ...