0

我想使用 TcpClient 和 TcpListener 通过网络发送 mp3 文件。我使用套接字实现了一个解决方案,但存在一些问题,所以我正在研究一种新的/更好的发送文件的方法。

我创建了一个如下所示的字节数组:length_of_filename|filename|file

然后应该使用上述类进行传输,但在服务器端,我读取的字节数组完全混乱,我不知道为什么。

我用来发送的方法:

 public static void Send(String filePath)
    {
        try
        {
            IPEndPoint endPoint = new IPEndPoint(Settings.IpAddress, Settings.Port + 1);
            Byte[] fileData = File.ReadAllBytes(filePath);
            FileInfo fi = new FileInfo(filePath);

            List<byte> dataToSend = new List<byte>();
            dataToSend.AddRange(BitConverter.GetBytes(Encoding.Unicode.GetByteCount(fi.Name))); // length of filename
            dataToSend.AddRange(Encoding.Unicode.GetBytes(fi.Name)); // filename
            dataToSend.AddRange(fileData); // file binary data


            using (TcpClient client = new TcpClient())
            {
                client.Connect(Settings.IpAddress, Settings.Port + 1);

                // Get a client stream for reading and writing.
                using (NetworkStream stream = client.GetStream())
                {
                    // server is ready 
                    stream.Write(dataToSend.ToArray(), 0, dataToSend.ToArray().Length);
                }
            }

        }
        catch (ArgumentNullException e)
        {
            Debug.WriteLine(e);
        }
        catch (SocketException e)
        {
            Debug.WriteLine(e);
        }
    }
}

然后在服务器端它看起来如下:

    private void Listen()
    {
        TcpListener server = null;
        try
        {
            // Setup the TcpListener
            Int32 port = Settings.Port + 1;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[1024];
            List<byte> data;

            // Enter the listening loop.
            while (true)
            {
                Debug.WriteLine("Waiting for a connection... ");
                string filePath = string.Empty;

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                using (TcpClient client = server.AcceptTcpClient())
                {
                    Debug.WriteLine("Connected to client!");
                    data = new List<byte>();

                    // Get a stream object for reading and writing
                    using (NetworkStream stream = client.GetStream())
                    {
                        // Loop to receive all the data sent by the client.
                        while ((stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            data.AddRange(bytes);
                        }
                    }
                }

                int fileNameLength = BitConverter.ToInt32(data.ToArray(), 0);
                filePath = Encoding.Unicode.GetString(data.ToArray(), 4, fileNameLength);
                var binary = data.GetRange(4 + fileNameLength, data.Count - 4 - fileNameLength);

                Debug.WriteLine("File successfully downloaded!");

                // write it to disk
                using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Append)))
                {
                    writer.Write(binary.ToArray(), 0, binary.Count);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }
    }

谁能看到我遗漏/做错的事情?

4

1 回答 1

4

损坏是由服务器上的以下代码引起的:

// Loop to receive all the data sent by the client.
while ((stream.Read(bytes, 0, bytes.Length)) != 0)
{
    data.AddRange(bytes);
}

stream.Read不会总是填满bytes缓冲区。如果 TCP 套接字没有更多可用数据,或者在读取消息的最后一个块时(除非它是缓冲区大小的精确倍数),它将不会被填充。

data.AddRange调用添加了所有内容bytes(假设它总是满的)。因此,这有时会最终添加来自上一次调用的数据stream.Read。要纠正这一点,您需要存储返回的字节数Read并仅添加此字节数:

int length;

while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
    var copy = new byte[length];
    Array.Copy(bytes, 0, copy, 0, length);
    data.AddRange(copy);
}

请注意,您可能希望重组代码以提高性能和内存使用率(并可能因此使其更易于阅读)。无需在发送之前将所有数据读取到客户端的内存中,您可以直接写入NetworkStream. 在服务器上,您不需要将所有内容从流复制到内存中。您可以读取 4 字节的文件名长度并对其进行解码,然后读取并解码文件名,最后将流的其余部分直接复制到 a FileStreamBinaryWriter不需要)。

还值得注意的是,您正在使用FileMode.Append. 这意味着发送的每个文件都将附加到同名的前一个副本。您可能想FileMode.Create改用,如果文件已经存在,它将覆盖。

于 2010-03-08T23:41:06.820 回答