简体   繁体   English

尝试将文件从C#客户端发送到node.js TCP服务器

[英]Trying to send a file from a C# client to a node.js TCP server

I have the following code: 我有以下代码:

C# client: C#客户端:

class Program
{
    static void Main(string[] args)
    {
        var client = new TcpClient(AddressFamily.InterNetwork);

        client.Connect("127.0.0.1", 9090);

        byte[] buffer = Encoding.Default.GetBytes(Program.ReadFile(@"C:\pub\image.jpg"));

        using (var stream = client.GetStream())
        {
            stream.Write(buffer, 0, buffer.Length);
        }

        Console.WriteLine("File sent.");

        Console.ReadLine();
    }

    public static string ReadFile(string path)
    {
        string content = string.Empty;

        using (var stream = new FileStream(path, FileMode.Open))
        {
            using (var reader = new StreamReader(stream))
            {
                content = reader.ReadToEnd();
            }
        }

        return content;
    }
}

node.js server: node.js服务器:

var net = require('net');
var fs = require('fs');

var server = net.createServer(function (socket)
{
    var buffer = new Buffer(0, 'binary');

    socket.on('data', function (data)
    {
        buffer = Buffer.concat([buffer, new Buffer(data, 'binary')]);
    });

    socket.on("end", function (data)
    {
        fs.writeFile("image.jpg", buffer);
        buffer = new Buffer(0, 'binary');
    });
});

server.listen(9090, '127.0.0.1');

This doesn't work. 这行不通。 The file always arrives corrupt. 该文件总是损坏。 What am I doing wrong? 我究竟做错了什么?

This is most probabbly encoding issue. 这是最可能的编码问题。

I would try to do: 我会尝试做的:

1) in C# : Encoding.UTF8.GetBytes(Program.ReadFile(@"C:\\pub\\image.jpg")); //EXPLICITLY SPECIFY UTF8 ENCODING 1)在C#Encoding.UTF8.GetBytes(Program.ReadFile(@"C:\\pub\\image.jpg")); //EXPLICITLY SPECIFY UTF8 ENCODING Encoding.UTF8.GetBytes(Program.ReadFile(@"C:\\pub\\image.jpg")); //EXPLICITLY SPECIFY UTF8 ENCODING

and

2) in JS : var buffer = new Buffer(0); 2)在JSvar buffer = new Buffer(0);

as, according to the node.js documentation : 作为,根据node.js 文档

Allocates a new buffer containing the given str. 分配一个包含给定str的新缓冲区。 encoding defaults to 'utf8'. 编码默认为“ utf8”。

Hope this helps. 希望这可以帮助。

Your problem is on how you are reading the file in C#. 您的问题在于如何在C#中读取文件。 If you write back the file to disk from C# you can see how the number of bytes does not match: 如果从C#将文件写回磁盘,则可以看到字节数不匹配:

// This code does not work
// the number of bytes in image2.jpg won't match image.jpg
byte[] buffer = Encoding.Default.GetBytes(Program.ReadFile(@"C:\pub\image.jpg"));
File.WriteAllBytes(@"c:\pub\image2.jpg", buffer);

Change your read to use the native C# ReadAllBytes instead: 将您的读取更改为使用本机C#ReadAllBytes代替:

// This works 
// the number of bytes in image2.jpg will match image.jpg
byte[] buffer = File.ReadAllBytes(@"C:\temp\file.jpg");
File.WriteAllBytes(@"c:\pub\image2.jpg", buffer);

Your Node.js code is correct and works once you send it the correct bytes from C#. 您的Node.js代码正确无误,并且一旦您从C#发送正确的字节就可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM