Multiple FTP commands via one connection

Another FTP question. :)

What's the best way to use the 2.0 Framework to send multiple FTP commands during one connection?

The FtpWebRequest class seems to make a separate connection for each command.

I'd like to connect/login once, send two or more FTP commands, receive the response to each, then disconnect.

Karen

[334 byte] By [KarenKenworthy] at [2008-2-24]
# 1
The current Ftp model is a request response model similar to http. It is a stateless implementation. So you have to create separate FtpWebRequest's for each and every request you make to the server. However if you have the keepAlive set to true and the credentials in the FtpWebRequest are the same we dont send a quit command after the request and will attempt to reuse the connection.. However even in this case a separate FtpWebRequest has to be created.
Malar at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
<< However if you have the keepAlive set to true and the credentials in the FtpWebRequest are the same we dont send a quit command after the request and will attempt to reuse the connection. >>

Thanks! I'm not worried about the client-side overhead of creating the FtpWebRequest object each time. But being able to avoid the login process for each command, and maintaining state on the FTP server (by continuing the same session) should be a big help.

BTW, when using this process, how do you finally end the connection? Will calling .Close on the FtpWebResponse object do it?

Or should I create an FtpWebRequest object, and specify "QUIT" as its .Method?

If so, could .Quit be added to the WebRequestMethods.Ftp enumeration?

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3
For that request where you want to close it you can set the keepalive to false
Did that work for you?
mahjayar at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4

System.Net has a concept of connection management using Servicepoints. http://blogs.msdn.com/adarshk/articles/345411.aspx.. Connections are reused. If the idle time on connection elapses the default idle time (can be changed by a property in servicepoint manager) then the connection is cleaned up. the socket is basically closed.. So you need not bother about sending QUIT explicitly..

Malar at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5
Malar wrote:
System.Net has a concept of connection management using Servicepoints. http://blogs.msdn.com/adarshk/articles/345411.aspx.. Connections are reused. If the idle time on connection elapses the default idle time (can be changed by a property in servicepoint manager) then the connection is cleaned up. the socket is basically closed.. So you need not bother about sending QUIT explicitly

Thanks for the explanation, and the pointer to the articles!

That makes a lot of sense for HTTP connections. But for FTP, not having an explicit way to end the connection wastes resources on the FTP server. The server has to maintain the state of the session until an explicit QUIT, or the connection times out -- often a few minutes.

I guess that explains why there are no Ftp method values for changing the current directory on the FTP folder. With connection pooling, there's no way to be sure the FTP session you're accessing when you issue your second command is the same as the one you access with your first. And there's no guarantee some other program hasn't issued commands over the connection you're using, between commands of your own.

So commands that depend on the state of the FTP session could have some unexpected results.

I really do appreciate the added FTP support. But I wonder if grafting it onto the Http protocol support was the best design decision. Besides the wasted server resources, and server state issues, it probably accounts for the .GetDateTimestamp/LastModified and GetFileSize/ContentLength confusion too.

And the weird "FtpWeb" prefix too. :)

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 6
mahjayar wrote:
For that request where you want to close it you can set the keepalive to false
Did that work for you?

Just tried a test, and I believe it did work!

After the first command, issued with KeepAlive true, the response object's ExitMessage was blank. After the second command, when KeepAlive was false, the ExitMessage property became "221".

If I leave the KeepAlive property at its default value during both commands, the ExitMessage property is always blank.

Oddly, the BannerMessage and WelcomeMessage properties remained the same after both commands. I sort of thought they might be blank after the second command. But I guess the connection pool remembers these for persisted connections?

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified