|
I am writing a FTP client program using TCP (connection- oriented) sockets. My question is why I only can send the first command to FTP server correctly, and the rest of commands are all return
"500 '': command not understood."
[code:1]/*
*
*/
#include <string.h>
#include "user.h"
#include "local.h"
main() {
int orig_sock, len, r;
static struct sockaddr_in serv_adr;
struct hostent *host;
len = BUFSIZ;
host = gethostbyname("ftp.servername.com"); /* GET INFO */
if (host == (struct hostent *) NULL ) {
perror("gethostbyname ");
exit(2);
}
memset(&serv_adr, 0, sizeof( serv_adr)); /* Clear it out */
serv_adr.sin_family = AF_INET;
memcpy(&serv_adr.sin_addr, host->h_addr, host->h_length);
serv_adr.sin_port = htons( PORT );
if ((orig_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){/* SOCKET */
perror("generate error");
exit(3);
}
/* CONNECT */
if (connect(orig_sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) < 0) {
perror("connect error");
exit(4);
}
r = send(orig_sock, user , sizeof(user),0);
if (r< 0)
{
perror("write error");
exit(5);
}
printf(" sent %d byte. \n",r);
read(orig_sock, buf, len);
printf("%s\n", buf);
write(fileno(stdout),"> ", 3);
r = send(orig_sock, pass, sizeof(pass),0);
if (r < 0)
{
perror("write error");
exit(5);
}
printf(" sent %d byte. \n",r);
read(orig_sock, buf, len);
printf("%s\n", buf);
printf("Quit...\n");
write(orig_sock, quit, sizeof(quit));
read(orig_sock, buf, len);
printf("%s\n", buf);
close(orig_sock);
exit(0);
}
[/code:1]
user is definded as "USER uname\r\n"
pass is definded as "PASS password\r\n"
and the output looks like:
220 FTP server (Digital UNIX Version 5.60) ready.
> User...
total is 16 byte... sent 16 byte.
331 Password required for uname.
> Pass...
500 '': command not understood.
> Quit...
500 '': command not understood.
But if I command out the uname part, which is send the "PASS password\r\n" first, the output for password is correct, but the rest of them are still wrong.
Thanks!!! |
|