【问题标题】:NodeJS v6.2.2 - TCP server receives extra blank spaces from client's string messageNodeJS v6.2.2 - TCP 服务器从客户端的字符串消息中接收到额外的空格
【发布时间】:2016-10-20 13:28:51
【问题描述】:

我开始学习 NodeJS,但遇到了一些初学者问题。

这是我的简单 TCP 服务器代码:

const net = require('net');
const StringDecoder = require('string_decoder').StringDecoder;


const PORT = 9000;
const ADDRESS = '127.0.0.1';

const server = net.createServer((socket) => {
    socket.on('data', (chunk) => {
        const decoder = new StringDecoder('utf8');
        const message = Buffer.from(chunk);
        console.log(decoder.write(message + ' length:' + message.length));
    });
}).listen(PORT, ADDRESS);

console.log('Server running at: %s:%s', ADDRESS, PORT);

server.on('connection', (socket) => {
    var playerAddress = socket.remoteAddress.toString() +':'+ socket.remotePort.toString();
    console.log('Player connected: %s', playerAddress);
});

还有 C# 客户端应用程序:

using System;
using System.Net.Sockets;

namespace SimpleTCPClient
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient("127.0.0.1", 9000);
            try
            {
                using (NetworkStream stream = client.GetStream())
                {
                    string message = "Borko";
                    byte[] bytes = new byte[message.Length * sizeof(char)];
                    System.Buffer.BlockCopy(message.ToCharArray(), 0, bytes, 0, bytes.Length);

                    stream.Write(bytes, 0, bytes.Length);
                }
            }
            catch (ObjectDisposedException)
            {
                Console.WriteLine("Lost connection to the server.");
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Application isn't connected to the server.");
            }
            finally
            {
                client.Close();
            }

        }
    }
}

运行服务器和客户端应用程序后,我得到了以下输出:

我找不到那些空白的解决方案。请帮忙。谢谢!

【问题讨论】:

    标签: c# node.js tcp


    【解决方案1】:

    你把字符串转换成字节数组的方式不对,下面的代码修复了你的多余空间问题:

    using System;
    using System.Net.Sockets;
    
    namespace SimpleTCPClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpClient client = new TcpClient("127.0.0.1", 9000);
                try
                {
                    using (NetworkStream stream = client.GetStream())
                    {
                        string message = "Borko";
                        byte[] bytes = Encoding.UTF8.GetBytes(message);
    
                        stream.Write(bytes, 0, bytes.Length);
                    }
                }
                catch (ObjectDisposedException)
                {
                    Console.WriteLine("Lost connection to the server.");
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("Application isn't connected to the server.");
                }
                finally
                {
                    client.Close();
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-17
      • 2019-04-15
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2014-09-08
      相关资源
      最近更新 更多