C# SFTP SSH.NET socket read operation has timed out after 30000 milliseconds

Under taker 1 Reputation point
2022-02-24T16:23:21.09+00:00

I am receiving random errors when uploading my file to SFTP. Say I have 100 files to upload, out of which 90-95 files are loading successful and rest 4-5 files are throwing error as below. Not sure what is causing the issue. Below is the code. It is failing at the step client.Connect();

Error Message: FileUpload Repository Error: Renci.SshNet.Common.SshOperationTimeoutException: Socket read operation has timed out after 30000 milliseconds. at Renci.SshNet.Abstractions.SocketAbstraction.Read(Socket socket, Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)

int port = 8022;
                if (int.TryParse(portString, out port))
                {
                    using (var client = new SftpClient(host, port, username, pd))
                    {
                        client.Connect();

                        if (!client.IsConnected)
                        {
                            throw new Exception("connection to the SFTP failed");
                        }

                        client.ChangeDirectory(FinalFolder);

                        using (var fs = new FileStream(sourcefile, FileMode.Open))
                        {
                            client.BufferSize = 4 * 1024;
                            client.UploadFile(fs, Path.GetFileName(sourcefile));
                            fs.Close();                          


                        }
                    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,925 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.