Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. normal FTP transfer? [modified]

normal FTP transfer? [modified]

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mahraja
    wrote on last edited by
    #1

    Hi everyone Does normal (not passive) FTP download needs threading? I mean when I make a data socket (bind, listen and then accept it) socket.accept is blocking and my program blocks and does no work anymore. should i seprate the socket.accept & the download process in two threads????

    modified on Monday, May 19, 2008 4:57 PM

    C E 2 Replies Last reply
    0
    • M mahraja

      Hi everyone Does normal (not passive) FTP download needs threading? I mean when I make a data socket (bind, listen and then accept it) socket.accept is blocking and my program blocks and does no work anymore. should i seprate the socket.accept & the download process in two threads????

      modified on Monday, May 19, 2008 4:57 PM

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      yes, you should.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • M mahraja

        Hi everyone Does normal (not passive) FTP download needs threading? I mean when I make a data socket (bind, listen and then accept it) socket.accept is blocking and my program blocks and does no work anymore. should i seprate the socket.accept & the download process in two threads????

        modified on Monday, May 19, 2008 4:57 PM

        E Offline
        E Offline
        Ed Poore
        wrote on last edited by
        #3

        The simple answer is no it does not need threading however as Christian pointed out, you should implement it.


        I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

        M 1 Reply Last reply
        0
        • E Ed Poore

          The simple answer is no it does not need threading however as Christian pointed out, you should implement it.


          I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

          M Offline
          M Offline
          mahraja
          wrote on last edited by
          #4

          sorry i can't understand what u mean, i should implement what?

          E 1 Reply Last reply
          0
          • M mahraja

            sorry i can't understand what u mean, i should implement what?

            E Offline
            E Offline
            Ed Poore
            wrote on last edited by
            #5

            You don't have to implement threading however if you do then you won't lock up your program while it downloads stuff. By implementing threading the program is more responsive and can deal with more things at once. However if this is for a quick script or something then there's probably no need to overcomplicate matters with having to deal with threading.


            I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

            M 1 Reply Last reply
            0
            • E Ed Poore

              You don't have to implement threading however if you do then you won't lock up your program while it downloads stuff. By implementing threading the program is more responsive and can deal with more things at once. However if this is for a quick script or something then there's probably no need to overcomplicate matters with having to deal with threading.


              I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

              M Offline
              M Offline
              mahraja
              wrote on last edited by
              #6

              Actually this is my code for downloading file from ftp server

              public void download(string remFileName, string locFileName, Boolean resume)
              {
              if (!logined)
              {
              login();
              }

                      setBinaryMode(true);
                      Console.WriteLine("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
                      ftpLog("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
              
                      if (locFileName.Equals(""))
                      {
                          locFileName = remFileName;
                      }
              
                      if (!File.Exists(locFileName))
                      {
                          Stream st = File.Create(locFileName);
                          st.Close();
                      }
              
                      FileStream output = new FileStream(locFileName, FileMode.Open);
              
                      createDataSocket(true);
                      if (dataSocket != null)
                      {
                          throw new Exception();
                      }
                      dataSocket = listeningSocket.Accept(); //###################
                      listeningSocket.Close();
                      listeningSocket = null;
                      if (dataSocket == null)
                      {
                          throw new Exception("Winsock error: " +
                                  Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
                      }
                                  
                      sendCommand("RETR " + remFileName);
              
                      if (!(retValue == 150 || retValue == 125))
                      {
                          throw new IOException(reply.Substring(4));
                      }
              
                      while (true)
                      {
                          bytes = dataSocket.Receive(buffer, buffer.Length, 0);
                          output.Write(buffer, 0, bytes);
              
                          if (bytes <= 0)
                          {
                              break;
                          }
                      }
              
                      output.Close();
                      
                      if (dataSocket.Connected)
                      {
                          dataSocket.Close();
                      }
                      
                      Console.WriteLine("");
              
                      readReply();
              
                      if (!(retValue == 226 || retValue == 250))
                      {
                          throw new IOException(reply.Substring(4));
                      }
              
                  }        
              

              and in the line signed with //########## the execution stops( or blocks, whatever). What can I do except implementation of threading?

              E T 2 Replies Last reply
              0
              • M mahraja

                Actually this is my code for downloading file from ftp server

                public void download(string remFileName, string locFileName, Boolean resume)
                {
                if (!logined)
                {
                login();
                }

                        setBinaryMode(true);
                        Console.WriteLine("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
                        ftpLog("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
                
                        if (locFileName.Equals(""))
                        {
                            locFileName = remFileName;
                        }
                
                        if (!File.Exists(locFileName))
                        {
                            Stream st = File.Create(locFileName);
                            st.Close();
                        }
                
                        FileStream output = new FileStream(locFileName, FileMode.Open);
                
                        createDataSocket(true);
                        if (dataSocket != null)
                        {
                            throw new Exception();
                        }
                        dataSocket = listeningSocket.Accept(); //###################
                        listeningSocket.Close();
                        listeningSocket = null;
                        if (dataSocket == null)
                        {
                            throw new Exception("Winsock error: " +
                                    Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
                        }
                                    
                        sendCommand("RETR " + remFileName);
                
                        if (!(retValue == 150 || retValue == 125))
                        {
                            throw new IOException(reply.Substring(4));
                        }
                
                        while (true)
                        {
                            bytes = dataSocket.Receive(buffer, buffer.Length, 0);
                            output.Write(buffer, 0, bytes);
                
                            if (bytes <= 0)
                            {
                                break;
                            }
                        }
                
                        output.Close();
                        
                        if (dataSocket.Connected)
                        {
                            dataSocket.Close();
                        }
                        
                        Console.WriteLine("");
                
                        readReply();
                
                        if (!(retValue == 226 || retValue == 250))
                        {
                            throw new IOException(reply.Substring(4));
                        }
                
                    }        
                

                and in the line signed with //########## the execution stops( or blocks, whatever). What can I do except implementation of threading?

                E Offline
                E Offline
                Ed Poore
                wrote on last edited by
                #7

                Can you use a TCP viewing utility thing to see the traffic that's going backwards and forwards. My thoughts are that you're not actually making the connection successfully to the FTP server and it's sitting there indefinitely waiting for a connection.


                I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

                1 Reply Last reply
                0
                • M mahraja

                  Actually this is my code for downloading file from ftp server

                  public void download(string remFileName, string locFileName, Boolean resume)
                  {
                  if (!logined)
                  {
                  login();
                  }

                          setBinaryMode(true);
                          Console.WriteLine("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
                          ftpLog("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
                  
                          if (locFileName.Equals(""))
                          {
                              locFileName = remFileName;
                          }
                  
                          if (!File.Exists(locFileName))
                          {
                              Stream st = File.Create(locFileName);
                              st.Close();
                          }
                  
                          FileStream output = new FileStream(locFileName, FileMode.Open);
                  
                          createDataSocket(true);
                          if (dataSocket != null)
                          {
                              throw new Exception();
                          }
                          dataSocket = listeningSocket.Accept(); //###################
                          listeningSocket.Close();
                          listeningSocket = null;
                          if (dataSocket == null)
                          {
                              throw new Exception("Winsock error: " +
                                      Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
                          }
                                      
                          sendCommand("RETR " + remFileName);
                  
                          if (!(retValue == 150 || retValue == 125))
                          {
                              throw new IOException(reply.Substring(4));
                          }
                  
                          while (true)
                          {
                              bytes = dataSocket.Receive(buffer, buffer.Length, 0);
                              output.Write(buffer, 0, bytes);
                  
                              if (bytes <= 0)
                              {
                                  break;
                              }
                          }
                  
                          output.Close();
                          
                          if (dataSocket.Connected)
                          {
                              dataSocket.Close();
                          }
                          
                          Console.WriteLine("");
                  
                          readReply();
                  
                          if (!(retValue == 226 || retValue == 250))
                          {
                              throw new IOException(reply.Substring(4));
                          }
                  
                      }        
                  

                  and in the line signed with //########## the execution stops( or blocks, whatever). What can I do except implementation of threading?

                  T Offline
                  T Offline
                  The Nightcoder
                  wrote on last edited by
                  #8

                  Hi, Just a couple of immediately obvious problems: 1. The Accept call will block until someone (the server) calls back. So... you should either use a separate thread (as suggested) or don't call Accept until you've sent the command to the server. 2. I can't see that you're sending the server the port number you're listening on (unless you've done this in sendCommand(), of course). If you don't, how can it call back? This doesn't apply to passive FTP, of course, but in passive FTP you don't listen - you connect. In any case, unless you're just practicing on how to use sockets - if you really need an FTP client and don't want to do all the details yourself: look at System.Net.WebClient and System.Net.FtpWebRequest in the framework documentation. Either of them will probably do what you want - and a lot easier. Pick the one you like...

                  -- Peter

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups