##Nodejs TCP 服务端和客户端

用客户端模拟STM32发送过来的字符串。主要是为了解决对字符串的处理。

##1.客户端代码

const net = require('net')
const client = net.connect({port:4001},() => {//向服务端发送
	console.log("connected to server!");
	client.write("{\"tem\":\"20\",\"hum\":\"80\"}");
});
client.on('data',(data) => {//从服务端接受
	console.log(data.toString());
	client.end();
});
client.on('end',() => {//断开连接
	console.log('disconnected from server');
})

发送的字符串是json格式:"{"tem":"20","hum":"80"}"

##3.服务端代码

var net = require('net')

net.createServer(function(socket){
	socket.on('data',function(data){//接受服务端数据
		console.log('got:',data.toString());

		var text = JSON.parse(data.toString());//将接收到的字符串转换成json对象
		console.log(text);//获取json对象
		console.log(text.tem);//获取tem的值
		console.log(text.hum);
	});
	socket.on('end',function(data){
		console.log('end');
	});
	socket.write('Ready to receive your message!')
	 
}).listen(4001);

@治电小白菜 20170317