locked
How do I detect whether a server needs active or passive ftp access from VB RRS feed

  • Question

  • I use an VB.NET ftp DLL from my application that allows passive or active ftp to be set (via a check-box in the calling app) while accessing an online server, but this option has to be correctly set before doing any ftp work with it and of course using the wrong setting leads to a session failure. Is it possible to 'request' whether a server requires active or passive protocol so that my system can self-set in code and thus make the manual option unnecessary, or do I need to know which protocol is required before I can make any contact at all? Seems to be a chicken-and-egg query, but I thought I'd ask!

    Thanks,
    Nick B

    Wednesday, October 3, 2012 10:42 AM

Answers

  • The internet has different layers with different protocols.  In your case you are dealing with 3 layers

    TCP - Transport layer

    HTTP - Protocol

    FTP - Application

     

    FTP is a type of HTTP message.  When http connects there is a negotiation that occurs where Headers are sent between Client and Server and a common mode is determine from the headers.  For example Microsoft would have webpages in English, Spainish, and French.  You webpage is set to English so when you connect to a microsoft webpage you are transfered to the English webpage.

    What you need to do is to connect to the server first using HTTPWebRequest and determine the connection mode.  You can then cast the  HttpWebrequest object to FtoWebRequest to expose the FTP functionality.

    See if this webpage will get you started

    http://msdn.microsoft.com/en-us/library/debx8sh9.aspx


    jdweng

    Wednesday, October 3, 2012 10:13 PM
  • If you connect on socket levell this isn`t a problem at all ,

    I once posted a Socket levell FTP class in this forum dig dig ,,,,,,

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/08b1e3ad-e9d1-4b80-9f92-11dcced1f0b8

    and there it is :-)

    with the information provided by the server on connection you can easy determine the state see the code in the link above which is verry good documented ;-)

    HTH


    My Code gallery contributions
    My site
    My Linkedin Profile

    Tuesday, October 9, 2012 11:03 AM

All replies

  • The internet has different layers with different protocols.  In your case you are dealing with 3 layers

    TCP - Transport layer

    HTTP - Protocol

    FTP - Application

     

    FTP is a type of HTTP message.  When http connects there is a negotiation that occurs where Headers are sent between Client and Server and a common mode is determine from the headers.  For example Microsoft would have webpages in English, Spainish, and French.  You webpage is set to English so when you connect to a microsoft webpage you are transfered to the English webpage.

    What you need to do is to connect to the server first using HTTPWebRequest and determine the connection mode.  You can then cast the  HttpWebrequest object to FtoWebRequest to expose the FTP functionality.

    See if this webpage will get you started

    http://msdn.microsoft.com/en-us/library/debx8sh9.aspx


    jdweng

    Wednesday, October 3, 2012 10:13 PM
  • Hi Joel,

    Many thanks for that - using VB's request and response I can now see a great range of information returned from the server ... but I'm still unclear as to what item(s) can tell me whether active or passive ftp exchanges are required. Probably a dumb question, but .... is there a simple aspect of the response that will indicate which ftp mode to set and subsequently use?

    Many thanks,
    Nick

    Tuesday, October 9, 2012 9:47 AM
  • See Wikio page below

    http://slacksite.com/other/ftp.html

    I would use wireshark to trace the interface as you work this problem. Wireshark will give yo uall the details.


    jdweng

    Tuesday, October 9, 2012 10:49 AM
  • If you connect on socket levell this isn`t a problem at all ,

    I once posted a Socket levell FTP class in this forum dig dig ,,,,,,

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/08b1e3ad-e9d1-4b80-9f92-11dcced1f0b8

    and there it is :-)

    with the information provided by the server on connection you can easy determine the state see the code in the link above which is verry good documented ;-)

    HTH


    My Code gallery contributions
    My site
    My Linkedin Profile

    Tuesday, October 9, 2012 11:03 AM
  • Michel : I saw that code last week in a Code Project.  Was the Code Project your code?

    jdweng

    Tuesday, October 9, 2012 11:10 AM
  • Thanks, I had seen that before, but I'm really just looking to get a simple VB answer - dumb engineer here! I have code that reliably sends / receives / deletes files and returns folder contents lists etc. with the 'active' flag set either to True or False before starting as required by the server, and this seems to work well - but what I'd really prefer is a line or two of code to determine whether active or passive mode should be set from the start. I can leave it to the user, but some are just not able to suss out whether to set a simple act/pass checkbox before they start, and an 'auto-setting' initiation would be a better solution.

    Nick

    Tuesday, October 9, 2012 11:22 AM
  • First send a command and listen on both the comand port (21) and data port (20). Then wait for response.  A Passive mode you will only get a response on the command port (21).  The active channel you will get a response on both the  command port (21) and data port(20).

    jdweng

    Tuesday, October 9, 2012 11:54 AM
  • Many thanks - I'll try that later.

    Nick

    Tuesday, October 9, 2012 12:40 PM
  • I don't see that you've been given an answer to this.  FTP Servers generally support both active and passive mode -- and default to active IIRC.

    It is only firewalls and NAT devices that care. Those devices often allow NO incoming connections to the protected PC(s). So using active mode doesn't work -- that's where the FTP server is 'active' in creating the data-transfer connection. In passive mode (when the client has send command PASV to the server) the server requests that the client to connect to it.

    Every(!) web browser uses passive mode. So that should be what use -- assuming you're on the Internet.

    If passive isn't working for you in some cases then investigate why? Check the server type and see if its one that doesn't support it...


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Tuesday, October 9, 2012 9:08 PM
  • Alan: Read the title of the posting.  Nick want to write software that can handle both a server that uses passive and active.  A secure server will fail when you try to login in passive mode.

    jdweng

    Tuesday, October 9, 2012 9:53 PM
  • Thanks Alan, it seems this is not so simple that a few lines of code will fix all likely situations. My application manages aerobatic championship scoring, where the frequent web results updates are usually via WiFi hotspots out on windy aerodromes and often from borrowed laptops with unknown setups by scorers who are not savvy about active vs. passive settings, so 'auto' ftp mode setting would be helpful but not essential. I can stick with the 'either/or' manual checkbox approach, but ....

    Wednesday, October 10, 2012 6:59 AM
  • I have never posted code on the codeproject website  ( in contrary to DevX , Planetsourcecode and  MSDN )

    I do not know who created the initial source it was out in the wild with no copyright statement or ownership claim what so ever .  

    Originally it was a console application , where the FTP part was in a module in a sort of VB6 style , so i converted it to this class  ( wich could use some modernization now also , auto properties ,shortened member initializers etc etc )

    So the class how it is now comes from my hand , I posted it several times as is and have it in use for several years now ( i guess something like 6 + years )


    My Code gallery contributions
    My site
    My Linkedin Profile

    Wednesday, October 10, 2012 8:28 AM
  • I was just curious if your were the originator.  I find a lot of cases where code that I wrote years ago ending up on other peoples websites.

    jdweng

    Wednesday, October 10, 2012 11:41 AM
  • Been there :-) 

    I was one of the first who published  working twitpic code in VB.Net ( when twitter was new and hot )  it has been forked and implemented in commercial products ( got some mails )

    My Silverlight upload control is also pretty popular  the Silverlight upload control has also been commercialized and improved by some   without donating the code back .

    But i do not really care , i had a problem or solved someone else`s problem and decided to donate that code to the wild after that it is out of my hands i get my glorry from the mails that i receive once in a while from some people who found my contact info in the comments and thank me for it  .

    Once the comunity was kind enough to share with me now it is my time to share something back  .

      


    My Code gallery contributions
    My site
    My Linkedin Profile

    Wednesday, October 10, 2012 5:07 PM