1 不安全的TCP通信
普通的TCP通信數(shù)據(jù)是明文傳輸?shù)?,所以存在?shù)據(jù)泄露和被篡改的風險,我們可以寫一段測試代碼試驗一下。
TCP Server:
const net=require('net');const server=net.createServer();const serverHost='127.0.0.1';const serverPort=8888;server.on('connection',(clientSocket)=>{ clientSocket.setEncoding('utf8'); clientSocket.on('data',(data)=>{ console.log(`client say:${data}`); }); clientSocket.on('error',()=>{});});server.listen({host:serverHost,port:serverPort},()=>{ console.log(`server is listening on port ${8888}`)});
TCP Client:
const net=require('net');const socket=new net.Socket();const serverHost='127.0.0.1';const serverPort=8888;let index=0;socket.on('error',()=>{});socket.connect({host:serverHost,port:serverPort},()=>{ console.log(`client has connected to host ${serverHost} , port ${serverPort}`); setInterval(()=>{ socket.write(`i love u ${index++}`); },3000);});