normal FTP transfer? [modified]
-
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
-
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
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 )
-
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
-
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
-
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
-
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
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?
-
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?
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
-
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?
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