zoukankan      html  css  js  c++  java
  • 创建TCP服务器和客户端

    // TCP 服务器;
    
    
    const net = require('net');
    let clientNo = 0;
    const server = net.createServer((client)=>{
        clientNo++;
        console.log(clientNo + '号客服端已连接');
        client.on('end',()=>{
            console.log(clientNo + '号客服端已连接');
        })
        client.write(clientNo + '号客服端,你好
    ');
        client.pipe(client);
        client.on('data',(data)=>{
            console.log(clientNo + '号客服端发来的数据 :' + data.toString());
        });
    });
    
    server.on('error',(err)=>{
        throw err;
    });
    
    server.listen(8234,()=>{
        console.log('TCP 服务器已启动');
    })
    
    // //  TCP  客户端;
    
    const net = require('net');
    var client = net.Socket(); // 创建TCP客户端
    // 设置连接的服务器
    client.connect(8234, '127.0.0.1', () => {
        console.log('连接到服务器');
        client.write('我是一个TCP客户端'); //向服务器发送数据
    });
    // 监听服务器传来的数据
    client.on('data', (data) => {
        console.log('服务器返回的数据:' + data.toString());
    });
    // 监听end事件
    client.on('end', () => {
        console.log('数据结束');
    });
    
    

  • 相关阅读:
    蜂窝网格的坐标以及寻路
    unity3d 第三人称视角的人物移动以及相机控制
    基本HTML结构
    平衡二叉树
    STL基础复习
    递归
    unity 傅老师学习
    blender基础操作
    最小生成树
    最短路径
  • 原文地址:https://www.cnblogs.com/d534/p/14802201.html
Copyright © 2011-2022 走看看