|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
How to write a FTP request
Currently I'm implementing a set of simple client side protocols.
For seding a HTTP request to server, my request is like this: request = "GET <HOST> HTTP/1.0 /r/n/r/n" But I dont know how should I write a FTP request for downloading a file. It's operated in binary mode. Besides, I would like to know how should I implement a FTP client side protocol. Is it like the HTTP protocol? Just do socket(), then connect(), then send request and receive request, and finally do closesocket()? But since FTP is using 2 connection (data & control) so I think should not be the same, right? I'm a newbie in socket programming. Can anyone helps? I'm writing all these in C++! Thanks in advance! |
|
#2
|
||||
|
||||
|
some sample commands
Code:
char *command[] = //these are the commands we send to ftp server
{
"USER anonymous\r\n",
"PASS asdf@aol.com\r\n",
"PASV\r\n", //passive mode for firewalls
"STOR file\r\n" //put the file onto the server
};
yes you have the general idea correct. the problem with transferring files is solved with the PASV command. in response to that the server will send you back it's ip and port number, than you connect to that to do the transfer. here is an example of one way to parse that reply, Code:
case 2: //PASV command; this causes the server to send us the port number
//that it opens for us to connect to, we log this for the next command
if( (buf[0] == '4') || (buf[0] == '5') )
{
printf(buf);
return 0;
}
//parse the reply and extract port number; format: (195,167,65,228,9,170).
//start from the end and count 3 commas to get here--------------/
//then add 2 to that to point at first byte of port (network byte ordered)
y = 0;
len = strlen(buf);
tmp = buf + len - 1; //the last nonNULL byte of buffer
while(y < 3) //find 2 ',' and 1 ')'
{
if( (tmp != NULL) && ( (*tmp == ',') || (*tmp == ')')) )//sanity check b4 dereference
{
y++;
*tmp = '\0'; //null terminate to make tokens
}
tmp--;
} //now tmp points 2 bytes behind start of port byte one
tmp += 2; //move back to first byte of port
*port = (u_char)(atoi(tmp)); //this is byte one
*(port + 1) = (u_char)(atoi(tmp + strlen(tmp) + 1)); //byte two
da_port = *(unsigned short *)port; //the port in n0rdered port
break;
i wrote a couple ftp clients if you want to check them out. well they're not exactly legitamate "clients" but you'll get the idea when you see them lemme know if ya want to check themout.
__________________
|
|
#3
|
||||
|
||||
|
In network programming, doing the sockets is the easy part. The application-level protocols is the hard part.
FTP is an application-level protocol and it is quite different from HTTP. For one thing, I think that it requires a session (logging on, authenticating, and issuing commands). Google on FTP protocol. Some of the hits I got are: The FTP Protocol Resource Center at http://war.jgaa.com/ftp/ Includes some source code and a list of the applicable and related RFCs. FTP: File Transfer Protocol at http://cr.yp.to/ftp.html FTP Protocol Related Documents at http://www.wu-ftpd.org/rfc/ wu-ftpd is the usual ftp server on Linux. Anyway, my Google search found over 3 million hits, so I suggest that your own Google would be a good place to start. BTW, RFC 959 appears to be the principal document governing how FTP is supposed to work and be used. You will definitely need to become familiar with it. Last edited by dwise1_aol : October 3rd, 2003 at 01:18 PM. |
|
#4
|
||||
|
||||
|
yea i almost forgot, definitly read the RFC. that's how i figured all the junk out.
![]() |
|
#5
|
|||
|
|||
|
Thanks guys...
|
![]() |
| Viewing: Dev Shed Forums > System Administration > FTP Help > How to write a FTP request |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|