Sending files over TCP/ .NET SSLStream is slow/not working

前端 未结 4 1292
终归单人心
终归单人心 2021-01-15 13:59

Im writing an Server/Client Application which works with SSL(over SSLStream), which has to do many things(not only file receiving/sending). Currently, It works

相关标签:
4条回答
  • 2021-01-15 14:30

    Your problem stems from the fact that you are performing what is essentially a binary operation through a text protocol and you are exacerbating that problem by doing it over an encrypted channel. I'm not going to re-invent this for you, but here are some options...

    1. Consider converting to an HTTPS client/server model instead of reinventing the wheel. This will give you a well-defined model for PUT/GET operations on files.

    2. If you can not (or will not) convert to HTTPS, consider other client/server libraries that provide a secure transport and well-defined protocol for binary data. For example, I often use protobuf-csharp-port and protobuf-csharp-rpc to provide a secure protocol and transport within our datacenter or local network.

    3. If you are stuck with your transport being a raw SslStream, try using a well-defined and proven binary serialization framework like protobuf-csharp-port or protobuf-net to define your protocol.

    4. Lastly, if you must continue with the framework you have, try some http-like tricks. Write a name/value pair as text that defines the raw-binary content that follows.

    0 讨论(0)
  • 2021-01-15 14:33

    I'd give a try of gzip compress before/after the network. From my experience, it helps. I'd say some code like this could help :

    using(GZipStream stream = new GZipStream(sslStream, CompressionMode.Compress)) 
    {
        stream.Write(...);
        stream.Flush();
        stream.Close();
    }
    

    Warning : It may interfer with SSL if the Flush is not done. and it will need some tests... and I didn't try to compile the code.

    0 讨论(0)
  • 2021-01-15 14:40

    First of all base64 over ssl will be slow anyway, ssl itself is slower then raw transport. File transfers are not done over base64 now days, http protocol is much more stable than anything else and most libraries on all platforms are very well stable. Base64 takes more size then actual data, plus the time to encode.

    Also, your following line may be a problem.

    ThreadInfos.wait.setvalue((csize / size) * 100);//outputs value to the gui
    

    If your this line is blocking, then this will slow down for every 4kb. Updating for every 4kb is also not right, unless a progress value from previous value differs by significant amount, there is no need to update ui for it.

    0 讨论(0)
  • 2021-01-15 14:44

    I think Akash Kava is right.

    while (cursize < size) {
        DateTime start = DateTime.Now;
        byte[] buffer = new byte[4096];
        readblocks = fs.Read(buffer, 0, 4096);
        ServerConnector.send("r", getBase64FromBytes(buffer));
        DateTime end = DateTime.Now;
        Console.Writline((end-start).TotalSeconds);
        cursize = cursize + Convert.ToInt64(readblocks);
        ThreadInfos.wait.setvalue((csize / size) * 100);
        end = DateTime.Now;
        Console.Writline((end-start).TotalSeconds);
    }
    

    By doing this you can find out where is the bottle neck.

    Also the way you sending data packets to server is not robust.

    Is it possible to paste your implementation of

    ThreadInfos.wait.setvalue((csize / size) * 100);

    0 讨论(0)
提交回复
热议问题