sshnet / SSH.NET

SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.
http://sshnet.github.io/SSH.NET/
MIT License
4.01k stars 934 forks source link

SSH.NET - Socket read operation has timed out after 30000 milliseconds. #889

Open kumudumushi-dev opened 3 years ago

kumudumushi-dev commented 3 years ago

Hi @drieseng I'm getting issue also on my SFTP setup using the SSH.NET latest version using .NET 5.0. It works fine until I got this issue.

Socket read operation has timed out after 30000 milliseconds.

This is my current code.

using (SftpClient sftpClient = new SftpClient(await getSftpConnection(export.Schedule.Host, export.Schedule.Username, export.Schedule.Password, export.Schedule.KeyFile, export.Schedule.Parapharase, export.Schedule.LogonType, log)))

    {
        try
        {
            if (!sftpClient.IsConnected)
            {
                log.LogInformation($"Connecting Sftp....");
                do
                {
                    try
                    {
                        sftpClient.Connect();
                    }
                    catch (Renci.SshNet.Common.SshConnectionException e)
                    {
                        log.LogInformation($"Connection error: {e.Message}");
                    }
                } while (!sftpClient.IsConnected);

                log.LogInformation($"Done connecting Sftp");
            }
                    //Send file per devicename
                    foreach (var _perDevice in export.ExportTablePerDevice)
                    {
                        log.LogInformation($"Device {_perDevice.DeviceName}");
                        try
                        {
                            var csv = export.Schedule.Layout.Equals("LAYOUT 05") ? ReportHelper.ConvertToDat(export.ExportTable) :
                                ReportHelper.ConvertToCsv(_perDevice.ExportTable, export.Schedule.Layout, _perDevice.TopUserHeader);

                            var stream = ZipArchiveExtensions.GenerateStreamFromString(csv);

                            string filename = "";
                            if (export.IsExportDevices)
                            {
                                filename = _perDevice.DeviceName.Replace(@"/", "_") + "_" + DateTime.Now.Ticks + "." + export.Schedule.ExportFormat.ToLower() ?? "csv";
                            }
                            else
                            {
                                    filename = (!String.IsNullOrEmpty(export.Schedule.FileName) ? export.Schedule.FileName.Replace(@"/", "_").Replace("#tick", DateTime.Now.Ticks.ToString()) :
                                       _perDevice.DeviceName.Replace(@"/", "_") + DateTime.Now.Ticks) + "." + export.Schedule.ExportFormat.ToLower() ?? "csv";
                            }
                            var file = $"/{export.Schedule.FilePath}/{filename}";
                            await sftpClient.UploadAsync(stream, file);
                        }
                        catch (Exception ex)
                        {
                            sftpClient.Disconnect();
                            sftpClient.Dispose();
                            log.LogInformation($"Device error: {ex.Message}");
                        }
                    }
            sftpClient.Disconnect();
            response = new StatusCodeResponse()
            {
                Code = HttpStatusCode.OK.ToString(),
                HttpStatus = (int)HttpStatusCode.OK,
                Message = $"Successfully sent export {export.Schedule.Name} via sftp server"
            };

        }
        catch (Exception ex)
        {
            sftpClient.Disconnect();
            log.LogInformation($"An exception has been caught {ex.Message}");
            response = new StatusCodeResponse()
            {
                HttpStatus = (int)HttpStatusCode.InternalServerError,
                Code = HttpStatusCode.InternalServerError.ToString(),
                Message = $"Error sending export {export.Schedule.Name} via sftp server: {ex.Message}"
            };
        }
    }
Jabsal commented 3 months ago

Has this issue been resolved ? What was the root cause and what did you do ?