Borko Djurovic
Borko Djurovic

Reputation: 313

NodeJS v6.2.2 - TCP server receives extra blank spaces from client's string message

I started learning NodeJS and I've got some beginners problems.

This is my simple TCP server code:

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);
});

And there is C# client application:

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();
            }

        }
    }
}

After running the server and client app I got this output: TCP Server output

I can't find solution for those blank spaces. Please help. Thanks!

Upvotes: 0

Views: 247

Answers (1)

leroydev
leroydev

Reputation: 2945

The way you convert the string into a byte array is wrong, the following code fixes your extra space problem:

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();
            }

        }
    }
}

Upvotes: 1

Related Questions