QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 779|回复: 8

Help!! 关于 FTP Client.

[复制链接]
发表于 2003-4-28 03:19:59 | 显示全部楼层 |阅读模式
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!!!
发表于 2003-4-28 08:12:03 | 显示全部楼层
u problem is that u use sizeof(user), sizeof(pass), and sizeof(quit). that return 4. u should use strlen(). to get the length of a string.
回复

使用道具 举报

 楼主| 发表于 2003-4-28 12:05:27 | 显示全部楼层
Thanks!!!!!
I have solved my question by using sprintf to combine cmds.

But I got another question about the data transfer.
I got error msg :
425 Can't build data connection: Connection refused.
when i used "RETR filename" or even "NLST".

Anyone know why? Do I need to create another connection for data connection? How should I do it?
回复

使用道具 举报

发表于 2003-4-28 14:04:34 | 显示全部楼层
maybe you didnot use the command "pasv".some servers use "pasv" before data transfer.
  I give you a suggestion.Use the programe"tcpdump"  to capture the packet that send by a ftp client such as "gftp". then decode the data to
ascii ,you can find the sequence of command .
回复

使用道具 举报

发表于 2003-4-28 21:24:00 | 显示全部楼层
[quote:2b33571710="partrick"]Thanks!!!!!
I have solved my question by using sprintf to combine cmds.

But I got another question about the data transfer.
I got error msg :
425 Can't build data connection: Connection refused.
when i used "RETR filename" or even "NLST".

Anyone know why? Do I need to create another connection for data connection? How should I do it?[/quote]
yes, ftp has two connection, one for command and one for data. u need read ftp protocol to know this. search ftp rfc on web, tons of them.
回复

使用道具 举报

 楼主| 发表于 2003-4-29 00:29:39 | 显示全部楼层
[quote:40be5eacbb="cheungming"]
yes, ftp has two connection, one for command and one for data. u need read ftp protocol to know this. search ftp rfc on web, tons of them.[/quote]

YES! that is my question. I have gone through the RFC 959, but I am still confuzing how to write the codes, how to build the data connection in detail.

I also tried the tcpdump, and it shows the sequence of data, but not the real cmds transfered.

What my cmds are:

USER uname  -ok
PASS password  -ok
SYST                   -ok
REST 0                -ok
PWD                    -ok
TYPE A                 -ok
PASV                    -ok
LIST                     -no responce from here.
TYPE I
PASV
RETR fileame
QUIT

If I don't use PASV mode, but PORT, I will get responce from LIST and RETR. But they will return error 425 can't build connection.
回复

使用道具 举报

发表于 2003-4-29 01:40:48 | 显示全部楼层
why not to find a simple ftp client source code to read? open source, open source, this is the value.  
回复

使用道具 举报

 楼主| 发表于 2003-5-1 11:33:46 | 显示全部楼层
I have figured it out.
I created a child process to build the data connection. And it works.
回复

使用道具 举报

发表于 2003-5-1 11:36:51 | 显示全部楼层
congs
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-16 01:40 , Processed in 0.201750 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表