C#Gzip错误地压缩服务器端的字节数组

时间:2018-12-04 11:32:28

标签: c# ssl server compression gzip

我有问题。当我尝试将压缩文件(byte [])发送到服务器时,以及对其进行解压缩时,文件彼此不匹配。我以为,问题出在压缩功能上,但是我的客户端可以在他这一边压缩和解压缩字节数组。所以,这是代码:

与服务器(客户端)的连接:

private void ConnectToServer() {
        try {
            _client = new TcpClient(_address, _port);
            _stream = new SslStream(_client.GetStream(), true,
                new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
            try {
                _stream.AuthenticateAsClient(_address);
                _stream.Write(new byte[]{1});
            } catch (AuthenticationException ex) {
                _iClient.ExceptionHandler(ex);
                _client.Close();
            }
        } catch (SocketException ex) {
            _iClient.ExceptionHandler(ex);
            _iClient.SuccessfulConnectionHandler();
            Environment.Exit(0);
        }
    }
}

接受客户端(服务器端):

private static void SetupServer() {
        try {
            IPAddress ipAddress;
            if (!IPAddress.TryParse(_address, out ipAddress)) ipAddress = Dns.GetHostAddresses(_address)[0];
            _listener = new TcpListener(ipAddress, _port);
            _listener.Start();
            WriteLine("\b\b\b - Успешно;");
            WriteLine("Ожидание подключений...");

            while (true) {
                var client = _listener.AcceptTcpClient();
                var sslStream = new SslStream(client.GetStream(), true);
                try {
                    sslStream.AuthenticateAsServer(new X509Certificate(_certificate, _password),
                        false, SslProtocols.Tls, true);
                    WriteLine("{0} : [{1}] Успешное подключение",
                        DateTime.Now.ToString(CultureInfo.InvariantCulture),
                        (client.Client.RemoteEndPoint as IPEndPoint)?.Address);
                    new Thread(() => ProcessClient(client, sslStream)).Start();
                } catch (AuthenticationException e) {
                    sslStream.Close();
                    client.Close();
                } 
            }
        } catch (Exception ex) {
            WriteLine(ex.Message);
            WriteLine(ex.StackTrace);
        } finally {
            _listener?.Stop();
        }
    }

还有我的压缩功能:

byte[] Compress1(byte[] data) {
        using (var compressedStream = new MemoryStream())
        using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) {
            zipStream.Write(data, 0, data.Length);
            zipStream.Close();
            return compressedStream.ToArray();
        }
    }

    byte[] Decompress1(byte[] data) {
        using (var compressedStream = new MemoryStream(data))
        using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
        using (var resultStream = new MemoryStream()) {
            zipStream.CopyTo(resultStream);
            return resultStream.ToArray();
        }
    }

所以。为什么会发生?

这是一些额外的信息:我使用ssl连接,只有大文件才有问题,我可以不压缩就保存文件。

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但是sslstream中有一些功能。最初,您只能获得1个字节(即使缓冲区大小更大),并且应该再次调用Read函数来读取剩余的字节。但这不是我的问题。一次调用读取函数可以获得的最大缓冲区大小为16227