简体   繁体   中英

c# Tcp client transfer file with sslStream

I try to send a file with an sslStream of tcp client. File is about 250kb. When i don't write dataLength to stream, server closes the connection i thing because of max receive buffer size. When i write into stream the dataLength, i have no exception but server has an error of type (connection closed gracefully). One reason is that, because the Tcp client close event, the server can not receive the data that i have sent; Here is my code. Server code is missing that i know that it has be writen in delphi with indy sockets library Here is my code

TcpClient sendClient = new TcpClient(serverName, port);
        LingerOption lingerOption = new LingerOption(true, 20);
        sendClient.LingerState = lingerOption;
        using (SslStream sendStream = new SslStream(sendClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
        {

            try
            {

                sendStream.AuthenticateAsClient(serverName, null, SslProtocols.Ssl2, true);


                sendStream.Write(Encoding.UTF8.GetBytes("Login\r\n" + username + "\r\n" + password + "\r\n"));
                sendStream.Flush();
                int bytesResp = -1;
                byte[] buffer = new byte[1024];

                bytesResp = sendStream.Read(buffer, 0, buffer.Length);

                string response = Encoding.UTF8.GetString(buffer, 0, bytesResp);

                if (response.Trim() == "OK")
                {

                    sendStream.Write(Encoding.ASCII.GetBytes("Send\r\n"));

                    FileStream inputStream = File.OpenRead(filePath);
                    FileInfo f = new FileInfo(filePath);
                    int size = unchecked((int)f.Length);


                    byte[] byteSize = Encoding.ASCII.GetBytes(size.ToString());

                    sendStream.Write(byteSize);
                    sendStream.Write(Encoding.ASCII.GetBytes("\r\n"));

                    sendStream.Write(Encoding.ASCII.GetBytes(fileName + "\r\n"));







                    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    {

                        long sum = 0;
                        int count = 0;
                        byte[] data = new byte[1024];
                        while (sum < size)
                        {
                            count = fs.Read(data, 0, data.Length);
                            sendStream.Write(data, 0, count);
                            sum += count;

                            Array.Clear(data, 0, data.Length);
                        }
                        sendStream.Flush();
                    }
                    sendStream.Write(Encoding.UTF8.GetBytes("\r\n"));
                    sendStream.Write(Encoding.UTF8.GetBytes("Quit\r\n"));


                }

                MessageBox.Show("end the procedure");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            finally
            {
                if (sendClient.Client.Connected)
                {
                    sendClient.Client.Disconnect(false);
                    sendClient.Client.Dispose();
                }
            }

I'm not sure if it will be of help but I had a similar issue with using(){} which for some reason it closed the sslstream. So potentially remove

using (SslStream sendStream = new SslStream(sendClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null)){}

into

SslStream sendStream = new SslStream(sendClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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