Home > Web Front-end > JS Tutorial > About the simple communication function between nodejs socket server and client

About the simple communication function between nodejs socket server and client

不言
Release: 2018-06-30 10:37:03
Original
2347 people have browsed it

This article mainly introduces the simple communication function between nodejs socket server and client in detail. It has certain reference value. Interested friends can refer to it.

The example of this article describes the use of node through node. The .js net module implements the simple communication function between the nodejs socket server and the client, and can be used as the client's port monitoring of the server and event receipt.

Server-side code

var net = require('net');
//模块引入
var listenPort = 8080;//监听端口
var server = net.createServer(function(socket){
 // 创建socket服务端
 console.log('connect: ' +
  socket.remoteAddress + ':' + socket.remotePort);
 socket.setEncoding('binary');
 //接收到数据
 socket.on('data',function(data){
  console.log('client send:' + data);
 });
socket.write('Hello client!\r\n');
 // socket.pipe(socket);
 //数据错误事件
 socket.on('error',function(exception){
  console.log('socket error:' + exception);
  socket.end();
 });
 //客户端关闭事件
 socket.on('close',function(data){
  console.log('client closed!');
   // socket.remoteAddress + ' ' + socket.remotePort);
 });
}).listen(listenPort);
//服务器监听事件
server.on('listening',function(){
 console.log("server listening:" + server.address().port);
});
//服务器错误事件
server.on("error",function(exception){
 console.log("server error:" + exception);
});
Copy after login

Client-side code

var net = require('net');
var port = 8080;
var host = '127.0.0.1';
var client= new net.Socket();
//创建socket客户端
client.setEncoding('binary');
//连接到服务端
client.connect(port,host,function(){
 client.write('hello server');
 //向端口写入数据到达服务端
});
client.on('data',function(data){
 console.log('from server:'+ data);
 //得到服务端返回来的数据
});
client.on('error',function(error){
//错误出现之后关闭连接
 console.log('error:'+error);
 client.destory();
});
client.on('close',function(){
//正常关闭连接
 console.log('Connection closed');
});
Copy after login

The running results are as follows

About the simple communication function between nodejs socket server and client

The above is the entire content of this article, I hope it will be helpful to everyone's study, more related content Please pay attention to PHP Chinese website!

Related recommendations:

About the way ajax handles cross-domain processing in jquery

About the template view mechanism of NodeJS framework Express Analysis

Introduction to path processing module path in Node.js

The above is the detailed content of About the simple communication function between nodejs socket server and client. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template