PDA

View Full Version : Connecting to a FTP with C#???


gicio
10-21-2002, 03:23 PM
Hi!

I check the MSDN Library but I can't find any infos about: how to connect to and FTP...
I need also infos how get folders names from the ftp....

Have any one experience which class can I use to connect to an FTP??


THX!!!

bwkaz
10-21-2002, 09:11 PM
FTP is easy. Err, well, sort of.

First you find documentation for the protocol -- what you actually send over the network. I'm not so sure where to look for this. Perhaps FreeBSD's netkit-ftp package? That would at least tell you more or less how it works.

Then, you get a socket opened in your OS (Unix and other BSD-like socket systems use the socket() syscall, Windows might have something similar, but I don't know). You connect() to the FTP port of the server, and send whatever you need over that link. In Unix anyway, sockets act just like file handles.

I don't know specifics, but I do know that if you aren't doing passive FTP, you open a local port (that is, bind() to it and listen() on it), then tell the server your IP or hostname, and that port. The server will then connect() to your port and send the file.

If you are using passive FTP, then you don't bother with a local port. You just ask for the file and the server tells you which port on it to connect() to in order to get it.

Of course, there are probably a million other details, but this is how it all works once your session is set up.

For "folder names" (a.k.a. directories), send an "ls" command to the server over your socket. It will respond, however that works with FTP, with a list of the files and a whole lot of other data. Send a "cd" to change directories, etc., etc. You might want to look at the "ftp" program -- connect to a server with it and figure out how the stuff all works. "help", I believe, gives help. "bye" quits.

gicio
10-22-2002, 08:21 AM
thanks

gearoid
12-16-2003, 12:16 PM
....but how do you do it???

I need to write a program to copy a file from an ftp site to my local machine. This file is Received as an array of bytes. But how do you convert this back to it's original type, e.g., .exe .zip, etc...??

Is there not an FTP library or something in the C# API? Or is there a higher level way of doing FTP??

thanks....

quatu
01-22-2004, 02:30 PM
gearoid >> Maybe you can use this code:


using System;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace project_ftp_client {

public class FTPClient {
public void ConnectFtpSite(string host, int port) {
try {
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPHostEntry iphost = Dns.GetHostByName(host);
IPAddress ip = iphost.AddressList[0];//ip of the host name
IPEndPoint iep = new IPEndPoint(ip , port);

s.Connect(iep);

string response;

SendRequest(s, "USER anonymous\r\n");
response = GetResponse(s);

// Find more FTP commands at http://www.freesoft.org/CIE/RFC/959/19.htm
SendRequest(s, "PASS **********\r\n");
response = GetResponse(s);
}
catch(Exception e) {
// Handle exception
}
}

private void SendRequest(Socket s, string cmd) {
s.Send(Encoding.ASCII.GetBytes(cmd), cmd.Length, 0);
}

private string GetResponse(Socket s) {
int sizeReceived;
string serverMessage;

Byte[] readBytes = new Byte[1024];
sizeReceived = s.Receive(readBytes);
serverMessage = Encoding.ASCII.GetString(readBytes, 0, sizeReceived);

return serverMessage;
}

}
}