unable to figure out the connection problem. Help.

Oct 11, 2012 at 9:08 PM

myClient = new FtpClient();
myClient.Host = serverURL;
myClient.Credentials = new System.Net.NetworkCredential(serverUser, serverPass);
myClient.EncryptionMode = serverSSL ? FtpEncryptionMode.Explicit : FtpEncryptionMode.None;
myClient.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
myClient.DataConnectionType = FtpDataConnectionType.PASV;

myClient.Connect();
myClient.SetDataType(FtpDataType.Binary);

foreach (String fileName in fileList)
{
    serverFileName = serverDir + Utilities.getFilenameFromFullname(fileName, outputFileFormat);
    raiseMessage(Messages.FTP_UPLOAD_BEGIN, new String[] { fileName, serverFileName });

    try
    {
 using (Stream ostream = myClient.OpenWrite(serverFileName))
 {
     const int buffer = 2048;
     byte[] contentRead = new byte[buffer];
     int bytesRead;
     using (FileStream istream = new FileInfo(fileName).OpenRead())
     {
  do
  {
      bytesRead = fs.Read(contentRead, 0, buffer);
      ostream.Write(contentRead, 0, bytesRead);
  } while (!(bytesRead < buffer));
  istream.Close();
     }
     ostream.Close();
     raiseMessage(Messages.FTP_FILE_UPLOAD_SUCCESS, new String[] { fileName });
 }
    }
    catch (Exception e)
    {
 raiseMessage(Messages.FTP_OPERATION_FAIL, null);
 raiseMessage(e.Message, null);
 raiseMessage(e.StackTrace, null);
    }
}

myClient.Disconnect();
return true;

Oct 11, 2012 at 9:16 PM

when encryptionMode = Implicit, it fails at the myClient.Connect() call itself with the following error.

Timed out trying to connect!
   at System.Net.FtpClient.FtpSocketStream.Connect(String host, Int32 port, Boolean allowIPv6)
   at System.Net.FtpClient.FtpClient.Connect()

when encryptionMode = Explicit, it fails with the exception

Timed out trying to read data from the socket stream!
   at System.Net.FtpClient.FtpSocketStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.FtpClient.FtpSocketStream.ReadLine(Encoding encoding)
   at System.Net.FtpClient.FtpClient.GetReply()
   at System.Net.FtpClient.FtpClient.Execute(String command)
   at System.Net.FtpClient.FtpClient.Connect()

 

Coordinator
Oct 11, 2012 at 9:26 PM

Can you provide a transaction log? Are you sure SSL is setup properly on the server? Does it work with other clients such as filezilla?

For info on getting the transaction log see the examples of FtpClient section in the CHM.

Oct 17, 2012 at 9:51 PM

I am having the same problem as described by diabloReigns..

  • When 'EncryptionMode' == Explicit, it fails with the exception "Timed out trying to read data from the socket stream!" during the "GetListing()" method. 
    • In this case, the client is able to connect just fine and validate the SSL certificate.
  • When 'EncryptionMode' == Implicit, it fails on the "Connect()" method by timing out (or hanging forever) during the "AuthenticateAsClient()" inside "ActivateEncryption()".

I'm trying to connect via SSL (SFTP) to port 990 on my FTP server.  FileZilla is able to connect just fine with the same host/port/credentials. Here is the transaction log from FileZilla upon connect...

Status: Connecting to 123.123.123.123:990...
Status: Connection established, initializing TLS...
Status: Verifying certificate...
Status: TLS/SSL connection established, waiting for welcome message...
Response: 220 GlobalSCAPE Enhanced File Transfer Server (v. 6.0.6)
Command: USER harrisonh
Response: 331 harrisonh
Command: PASS ***********
Response: 230 Login OK. Proceed.
Command: SYST
Response: 215 UNIX Type: L8
Command: FEAT
Response: 211-Features supported:
Response: COMB target;source_list
Response: REST STREAM
Response: SIZE
Response: MDTM
Response: XCRC filename;start;end
Response: SSCN
Response: MLST Size*;Modify*;Create;Type*;Unique;Perm*;Lang;Media-Type;CharSet;
Response: MODE Z 
Response: XNOP 
Response: 211 END
Command: PBSZ 0
Response: 200 PBSZ Command OK. Protection buffer size set to 0.
Command: PROT P
Response: 200 PROT Command OK. Using Private data connection
Status: Connected
Status: Retrieving directory listing...
Command: PWD
Response: 257 "/" is current folder.
Command: TYPE I
Response: 200 Type set to I.
Command: PASV
Response: 227 Entering Passive Mode (216,205,228,150,109,96).
Command: MLSDResponse: 150 Opening BINARY mode data connection for MLSD /.
Response: 226 Transfer complete. 3498 bytes transferred. 3498 bps.
Status: Directory listing successful

Coordinator
Oct 17, 2012 at 9:52 PM
Thanks, I'll look into it and get back as soon as possible.
Coordinator
Oct 17, 2012 at 9:58 PM

Are you by chance using the Begin* methods?

Coordinator
Oct 17, 2012 at 10:03 PM

Also, if you don't mind I'd like to know the server software and version.

Oct 17, 2012 at 10:03 PM

No, my code looks like...

using (FtpClient ftp = new FtpClient()) {                   

ftp.ReadTimeout = 5000;
ftp.Host = "ftphost.com";
ftp.DataConnectionEncryption = true;               
ftp.Credentials = new NetworkCredential("user", "pass", "domain");               
ftp.Port = 990;               
ftp.EncryptionMode = FtpEncryptionMode.Explicit;               
ftp.DataConnectionType = FtpDataConnectionType.AutoPassive;             
ftp.ValidateCertificate += new FtpSslValidation(ftp_ValidateCertificate);

        //ftp.Connect();

foreach (FtpListItem item in ftp.GetListing(workingDirectory, FtpListOption.Modify | FtpListOption.Size))                   
{                       
toReturn.Add(item.FullName);
        }

//ftp.Disconnect();

}

Oct 17, 2012 at 10:08 PM

I think it's...

GlobalSCAPE Enhanced File Transfer Server (v. 6.0.6)

Oct 17, 2012 at 10:10 PM

(On a Windows Server box)

Coordinator
Oct 17, 2012 at 10:15 PM

Please try disabling thread safe data connections cl.EnableThreadSafeDataConnections = false and let me know the result. 

Oct 17, 2012 at 10:17 PM

It didn't change anything. Same result..

Coordinator
Oct 17, 2012 at 10:20 PM

Alright, I'll have to try out some other ftp server software and see if I can reproduce the problem. I can't make it hang on filezilla server so it's hard to debug. Will most likely be tomorrow before I can really dig into it.

Coordinator
Oct 17, 2012 at 10:24 PM

I just pushed up a new revision with a slight change to the AuthenticateAsClient() call I'd like you to try. Let me know the results.

Oct 17, 2012 at 10:52 PM

I saw your revision. I actually already tried making that exact same change. It makes no difference so I changed it back to the way you had it before. I tried...

 

m_sslStream.AuthenticateAsClient(targethost, null, SslProtocols.Tls | SslProtocols.Ssl2 | SslProtocols.Ssl3, true);


...and...

m_sslStream.AuthenticateAsClient(targethost, null, SslProtocols.Tls | SslProtocols.Ssl2 | SslProtocols.Ssl3, false);

 

Neither of those changes made any difference..

 
Oct 17, 2012 at 10:54 PM

Actually, that worked! (Once I changed "Implicit" back to "Explicit") 

Thank you very much kind developer!

Oct 17, 2012 at 10:59 PM

Oops, sorry I was confused. I'm still having the same issue. What worked was the "Execute()" command, not the "GetListing" function.

Here is the call stack...

  [External Code] > System.Net.FtpClient.dll!System.Net.FtpClient.FtpSocketStream.ActivateEncryption(string targethost) Line 525 + 0x26 bytes C#  System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.OpenPassiveDataStream(System.Net.FtpClient.FtpDataConnectionType type, string command, object[] args) Line 835 + 0x10 bytes C# 
System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.OpenPassiveDataStream(System.Net.FtpClient.FtpDataConnectionType type, string command, object[] args) Line 785 + 0x13 bytes C#  System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.OpenDataStream(string command, object[] args) Line 970 + 0x13 bytes C#  System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.GetListing(string path, System.Net.FtpClient.FtpListOption options) Line 1404 + 0x72 bytes C#  FogSyncerNotifier.exe!
FogSyncerNotifier.FTPHandler.GetFTPFileDetails(string ftpHostAddressAndPath) Line 300 + 0x10 bytes C#  FogSyncerNotifier.exe!
FogSyncerNotifier.Form1.btnSync_Click(object sender, System.EventArgs e) Line 154 + 0x10 bytes C#  [External Code]   FogSyncerNotifier.exe!
FogSyncerNotifier.Program.Main(string[] args) Line 21 + 0x20 bytes C#  [External Code]

Coordinator
Oct 17, 2012 at 11:06 PM
Yeah I think the authentication may need to take place when the first read from the socket takes place, maybe. Will try to get my hands on globscape and test it out.

Sent from my iPhone

On Oct 17, 2012, at 5:00 PM, radman38654 <notifications@codeplex.com> wrote:

From: radman38654

Oops, sorry I was confused. I'm still having the same issue. What worked was the "Execute()" command, not the "GetListing" function.

Here is the call stack...

[External Code] > System.Net.FtpClient.dll!System.Net.FtpClient.FtpSocketStream.ActivateEncryption(string targethost) Line 525 + 0x26 bytes C# System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.OpenPassiveDataStream(System.Net.FtpClient.FtpDataConnectionType type, string command, object[] args) Line 835 + 0x10 bytes C#
System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.OpenPassiveDataStream(System.Net.FtpClient.FtpDataConnectionType type, string command, object[] args) Line 785 + 0x13 bytes C# System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.OpenDataStream(string command, object[] args) Line 970 + 0x13 bytes C# System.Net.FtpClient.dll!
System.Net.FtpClient.FtpClient.GetListing(string path, System.Net.FtpClient.FtpListOption options) Line 1404 + 0x72 bytes C# FogSyncerNotifier.exe!
FogSyncerNotifier.FTPHandler.GetFTPFileDetails(string ftpHostAddressAndPath) Line 300 + 0x10 bytes C# FogSyncerNotifier.exe!
FogSyncerNotifier.Form1.btnSync_Click(object sender, System.EventArgs e) Line 154 + 0x10 bytes C# [External Code] FogSyncerNotifier.exe!
FogSyncerNotifier.Program.Main(string[] args) Line 21 + 0x20 bytes C# [External Code]

Oct 17, 2012 at 11:24 PM

I found someone who suggests the following specific order to resolve...

"The correct sequence (for those that come after) was PASV command,
open socket/stream, send command to request data, validate stream."

 
it's the last comment @...

http://www.usenetmessages.com/view.php?c=microsoft&g=5441&id=63798&p=0

Coordinator
Oct 18, 2012 at 12:11 AM
That may be the case but I think the same problem that causes Implicit SSL to break is what is causing SSL on the data connection to break.
Coordinator
Oct 18, 2012 at 2:22 PM

OK, I got a trial version of GlobScape's secure FTP Server and here's what I've found. Implicit SSL connections work fine, not sure what's going on with your setup, maybe it has something to do with the key? How many bits is the key (so that we check that .net's SslStream supports it)?

There was indeed a bug with the command sequencing in the data channel ssl code and their ftp server. I moved the authentication after the command is executed and it now works with GlobeScape's server and still works with filezilla. I pushed that revision up this morning, if you don't mind please try it out and let me know if it gets you moving forward.

Oct 18, 2012 at 6:30 PM

Success! Thanks for your help!

Coordinator
Oct 18, 2012 at 6:31 PM

No problem

Mar 13, 2013 at 7:47 PM
Help! I am having a similar issue with sftp login. I keep getting this error "Timed out trying to read data from the socket stream!"

Following is the code

var ftp = new System.Net.FtpClient.FtpClient
                          {

                              Host = "xxx.host.com",
                              Port = 1012,
                              Credentials = new NetworkCredential("user", "pwd")
                              ,DataConnectionEncryption  = true,
                              DataConnectionType = FtpDataConnectionType.AutoPassive      
                              ,ConnectTimeout = 180000,ReadTimeout = 4000
                              ,EncryptionMode = FtpEncryptionMode.Explicit,

                          };


            ftp.Connect(); // Error Here
FYI - I am not using any certificates

I successfully tried using filezilla and following is the log ( I masked the actual credentials and host name)
Status: Connecting to xxx.host.com:1012...
Response: fzSftp started
Command: open "user@ xxx.host.co" 1012
Command: Pass: **********
Status: Connected to xxx.host.co
Status: Retrieving directory listing...
Command: pwd
Response: Current directory is: "/"
Status: Directory listing successful
Mar 13, 2013 at 8:47 PM
Help! I am having a similar issue with sftp login. I keep getting this error "Timed out trying to read data from the socket stream!"

Following is the code

var ftp = new System.Net.FtpClient.FtpClient
                          {

                              Host = "xxx.host.com",
                              Port = 1012,
                              Credentials = new NetworkCredential("user", "pwd")
                              ,DataConnectionEncryption  = true,
                              DataConnectionType = FtpDataConnectionType.AutoPassive      
                              ,ConnectTimeout = 180000,ReadTimeout = 4000
                              ,EncryptionMode = FtpEncryptionMode.Explicit,

                          };


            ftp.Connect(); // Error Here
FYI - I am not using any certificates

I successfully tried using filezilla and following is the log ( I masked the actual credentials and host name)
Status: Connecting to xxx.host.com:1012...
Response: fzSftp started
Command: open "user@ xxx.host.co" 1012
Command: Pass: **********
Status: Connected to xxx.host.co
Status: Retrieving directory listing...
Command: pwd
Response: Current directory is: "/"
Status: Directory listing successful
Coordinator
Mar 13, 2013 at 8:57 PM
This code doesn't support SFTP at all. It supports FTP and FTPS (FTP with SSL/TLS).
Mar 13, 2013 at 9:08 PM
Do you know any libraries that support SFTP with c#?
Coordinator
Mar 13, 2013 at 9:09 PM
They exist, search codeplex.
Mar 13, 2013 at 9:12 PM
thanks appreciate your taking the time.


On Wed, Mar 13, 2013 at 4:09 PM, jptrosclair <notifications@codeplex.com> wrote:

From: jptrosclair

They exist, search codeplex.

Read the full discussion online.

To add a post to this discussion, reply to this email (netftp@discussions.codeplex.com)

To start a new discussion for this project, email netftp@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com