English 中文(简体)
Using TCPClient with RAW FTP to retrieve file
原标题:

I get the following message back when trying to retrieve a file using TCPClient and RAW FTP:

425 Failed to establish connection.

I connect using :

using (TcpClient client = new TcpClient("ServerName", 21))
using (NetworkStream stream = client.GetStream())
using (StreamReader reader = new StreamReader(stream))
using (StreamWriter writer = new StreamWriter(stream))
{

Then I login to the server fine using USER **** and PASS ***. I manage to change to the correct directory using CWD /**/**, as mentioned this all works fine and i get the expected response messages.

However when the following code executes the message mentioned above is all i receive.

lineToSend = "PASV";
Program.logger.Add("Sending to server: " + lineToSend, 1);
writer.WriteLine(lineToSend);
while (!stream.DataAvailable)
{
    Program.logger.Add(stream.DataAvailable.ToString(), 1);
    Program.logger.Persist();
    Thread.Sleep(1000);

}
lineWeRead = reader.ReadLine();
Program.logger.Add("Received from server: " + lineWeRead, 1);
Program.logger.Persist();

lineToSend = "RETR file.txt";
Program.logger.Add("Sending to server: " + lineToSend, 1);
writer.WriteLine(lineToSend);
while (!stream.DataAvailable)
{
    Program.logger.Add(stream.DataAvailable.ToString(), 1);
    Program.logger.Persist();
    Thread.Sleep(1000);
}

Program.logger.Add("Loop Exited: " + stream.DataAvailable.ToString(), 1);
Program.logger.Persist();
lineWeRead = reader.ReadLine();
Program.logger.Add("Received from server: " + lineWeRead, 1);
Program.logger.Persist();

I am fairly new to C#.NET and have been asked specifically to use this method of connecting and not the httpWebRequest.

The file I am trying to receive is a text file containing only "Get this message" but will be bigger when used. Any help is much appreciated.

最佳回答

You might want to read up on the FTP protocol. There is not one connection, but two involved. I don t see a data connection anywhere in your code, which is probably where the 425 is coming from.

I agree with the comment -- unless this is a homework-style question, please don t reinvent the wheel here. FTP stinks and this problem has been solved.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签