Sangam Uprety
Sangam Uprety

Reputation: 1482

FTPClient - Timed out trying to read data from the socket stream

I am trying to connect to secured ftp server in vb.net using FTPClient. I am able to connect and upload/download data through filezilla. But from .net code, I am having timeout issue. Did I make any mistake, or is there anything missing in my following code?

Public Function ftpDownload(ByVal strFileName As String) As FileStream
        Try
            Dim client As New FtpClient("ftp://xxx.xxx.xxx.xxx")
            client.Port = 990
            client.Credentials = New NetworkCredential("myusername", "mypassword")
            client.EncryptionMode = FtpEncryptionMode.Explicit
            client.DataConnectionEncryption = True
            client.ReadTimeout = 20000
            client.DataConnectionType = FtpDataConnectionType.AutoPassive
            'System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

            client.Connect()

            Dim arr As New MemoryStream()
            client.Download(arr, strFileName)
            Using responseStream As IO.Stream = arr
                Using fs As New IO.FileStream("c:\temp\temp.123", FileMode.Create)
                    Dim buffer(2047) As Byte
                    Dim read As Integer = 0
                    Do
                        read = responseStream.Read(buffer, 0, buffer.Length)
                        fs.Write(buffer, 0, read)
                    Loop Until read = 0
                    responseStream.Close()
                    'fs.Flush()
                    'fs.Close()
                    Return fs
                End Using
                responseStream.Close()
            End Using
        Catch ex As Exception
            MsgBox(ex)
            Return Nothing
        End Try

It is throwing exception from client.Connect(). Following is the screenshot of the exception as seen in quick watch window:

enter image description here

Upvotes: 1

Views: 3714

Answers (2)

tkburbidge
tkburbidge

Reputation: 371

When using port 990 you cannot use Explicit EncryptionMode. Use Implicit EncryptionMode if you need to connect to port 990.

Upvotes: 0

Westonsupermare
Westonsupermare

Reputation: 108

The error message makes it look to me like your timeout is too low. Try setting a ridiculously long timeout and seeing if you are getting any traffic at all. Worst case scenario, use wireshark and compare your requests to those from FileZilla. VB isn't the best at manual packet manipulation, but there might be something wrong in your configuration that would be revealed by comparing the request packets.

Upvotes: 0

Related Questions