【问题标题】:How to fix corrupt byte[]-s in win socket如何修复win socket中损坏的字节[]-s
【发布时间】:2016-05-13 09:22:53
【问题描述】:

我有两个 win 套接字应用程序,服务器和客户端。服务器应用程序在我的虚拟机上,客户端在主机上,通信正常。我正在通过该套接字发送一个 ISO 文件 (700MB),我遇到了接收到的字节已损坏的错误。当我的文件来到虚拟机时,它具有原始大小,但内容不正确。在 客户端 端,我正在使用以下代码:

public class ProgramClient
    {
        public static void StartClient()
        {
            // Data buffer for incoming data.
            byte[] msg;
            try
                {
                    IPAddress ipAd = IPAddress.Parse("192.168.137.71");
                    IPEndPoint remoteEP = new IPEndPoint(ipAd, 1234);
                    Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    sender.Connect(remoteEP);

                    Console.WriteLine("Client connected to {0}", sender.RemoteEndPoint.ToString());
                    Console.WriteLine("Sending file...");
                    msg = GetBinaryFile(@"C:\TCPIP\test_big.iso");

                    byte[] msgLengthBytes = BitConverter.GetBytes(msg.Length-3);
                    int msgLength = BitConverter.ToInt32(msgLengthBytes, 0);
                    Console.WriteLine("int: {0}", msgLength);
                    Console.WriteLine("msgL size: {0}", msgLengthBytes.Length);

                    //join arrays, file size info, TCP header
                    byte[] result = new byte[msgLengthBytes.Length + msgLength];
                    Buffer.BlockCopy(msgLengthBytes, 0, result, 0, msgLengthBytes.Length);
                    Buffer.BlockCopy(msg, 3, result, msgLengthBytes.Length, msgLength);

                    //file extension info, TCP Header
                    byte extension = 2; //file extension code
                    byte[] newArray = new byte[result.Length + 1];
                    result.CopyTo(newArray, 1);
                    newArray[0] = extension;
                    result = newArray;

                    int bytesSent = sender.Send(result);
                    Console.WriteLine("result size: {0}", result.Length);

                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();

                    Console.WriteLine("\nPress ENTER to continue...");
                    Console.Read();

                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }
        }

        private static byte[] GetBinaryFile(string filename)
        {
             byte[] bytes;
             using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
             {
                  bytes = new byte[file.Length];
                  file.Read(bytes, 0, (int)file.Length);
             }
             return bytes;
        }

        public static void Main(String[] args)
        {
            StartClient();
        }
    }

在 服务器 端,我有以下代码:

class ProgramServer
    {

        public static void Main(String[] args)
        {
            try
            {
                StartListening();
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException : {0}", se.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }
        }

        public static void StartListening()
        {
            byte[] bytes = new Byte[1024];

            while (true)
            {
                string outputPath = string.Empty;
                outputPath = @"C:\output\output";
                Console.WriteLine("Waiting for a connection...");
                Socket handler = SocketInstance().Accept();
                data = null;

                //for the TCP header, get file extension
                bytes = new byte[1];
                int bytesReceivedExtension = handler.Receive(bytes);
                string extension = GetExtension(bytes[0]);
                outputPath = outputPath + extension;

                //for the TCP header, get file size information
                bytes = new byte[4];
                int bytesReceived = handler.Receive(bytes);
                int Lenght = BitConverter.ToInt32(bytes, 0);
                Console.WriteLine("msg length: " + Lenght);
                int TotalReceivedBytes = 0;

                while (TotalReceivedBytes < Lenght)
                {
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    TotalReceivedBytes = TotalReceivedBytes + bytesRec;
                    AppendAllBytes(outputPath, bytes);    
                }
                Console.WriteLine("Bytes received total: " + TotalReceivedBytes);
                Console.WriteLine(File.Exists(outputPath) ? "File received." : "File not received.");
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            Console.WriteLine("\nPress ENTER to continue...");
            Console.Read();
        }

        private static Socket SocketInstance()
        {
            IPAddress ipAd = IPAddress.Parse("192.168.137.71");
            IPEndPoint localEndPoint = new IPEndPoint(ipAd, 1234);
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(localEndPoint);
            listener.Listen(10);
            return listener;
        }

        public static void AppendAllBytes(string path, byte[] bytes)
        {
            using (var stream = new FileStream(path, FileMode.Append))
            {
                stream.Write(bytes, 0, bytes.Length);
            }
        }

        public static string GetExtension(byte extOfFile)
        {
            switch (extOfFile)
            {
                case 0:
                    return ".txt";
                case 1:
                    return ".png";
                case 2:
                    return ".iso";
                default:
                    return "";
            }
        }
    }

那么,我如何确定我的 byte[] 没问题?因为当我在接收端打开那个 ISO 文件时,它的内容是不正确的。是否有任何类型的文件到二进制转换的替代方法? 谢谢。

【问题讨论】:

  • 为什么要读取内存中的整个文件?这不是真正的可扩展性和资源浪费。无论如何,您可能根本没有对Send() 或Receive() 的返回值做任何事情。另请参阅Why does my client socket not receive what my server socket sends?。
  • 这仅用于测试目的,我知道这是浪费资源。的返回值很好。我在接收端得到了 700MB 的文件,但是当我打开它时,由于某种原因它不正常。 @CodeCaster
  • 我的意思是您的问题中几乎没有任何相关代码。请阅读How to Ask 并提供minimal reproducible example。字节不会因传输而“损坏”,问题必须出在您的发送方或接收方。使用较小的文件进行测试并准确地描述字节是如何损坏的。比如是否缺少字节,是否有太多字节,字节的值是否与您预期的不同,等等。
  • 好的,我把我所有的代码都放在这里了。 @CodeCaster

标签: sockets bytearray


【解决方案1】:

您制定的框架协议似乎是这样工作的:

 0  1  2  3  4  ...  N
[L][L][L][L][D][...][D]

其中L 表示一个 32 位整数(采用哪种字节序?),表示Data 的长度。

首先,您发送的文件长度错误:

byte[] msgLengthBytes = BitConverter.GetBytes(msg.Length-3);

为什么要减去 3?你不应该。这会导致文件的最后 3 个字节被截断。

然后在填充消息缓冲区时,从第 3 个字节开始写入,也就是 L 的最后一个字节:

Buffer.BlockCopy(msg, 3, result, msgLengthBytes.Length, msgLength);

这将导致阅读器解释错误的数据长度。您应该从第 4 个字节开始。

第三,写入文件时,不应该追加整个缓冲区,而只追加Receive()实际写入缓冲区的字节:

bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
TotalReceivedBytes = TotalReceivedBytes + bytesRec;
AppendAllBytes(outputPath, bytes, bytesRec);    

然后在那个方法中:

public static void AppendAllBytes(string path, byte[] bytes, int bufferLength)
{
    using (var stream = new FileStream(path, FileMode.Append))
    {
        stream.Write(bytes, 0, bufferLength);
    }
}

这就是为什么如果您不太清楚自己在做什么,就不应该编写自己的协议和套接字代码。改为利用现有协议和库。

【讨论】:

  • 嗨。首先,感谢您的回答。起初我发送一个 txt 文件,我得到了 3 个匿名字节 (???) 所以这就是我减去 3 的原因。好吧,除了连接字节部分,其他一切都好吗?谢谢。
  • @Ferid 是的,还有第三个问题,请参阅编辑。您的文本文件可能有 1021 字节长。
猜你喜欢
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多