LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1261|回复: 11

小弟第一次编socket程序.一个ftp的login的子程序.有点问题.请教大家

[复制链接]
发表于 2003-7-12 20:15:29 | 显示全部楼层 |阅读模式
程序片断如下:
c语言的.在cygwin环境下写的.
[PHP]
int Login ()
{
  char user[] = "anonymous";
  char pass[] = "someone@21cn.com";
  strcpy (sbuf, "USER ");
  strcat (sbuf, user);
  printf (sbuf);
  strcat (sbuf, "\r\n");
  send (cmd_sock, sbuf, strlen (sbuf), 0);
  recv (cmd_sock, rbuf, sizeof (rbuf), 0);
  printf (rbuf);
  if (CheckReplycode (rbuf, "331") != 0)
    return -1;
  memset (rbuf, 0, sizeof (rbuf));
  strcpy (sbuf, "ASS ");
  strcat (sbuf, pass);
  printf (sbuf);
  strcat (sbuf, "\r\n");
  send (cmd_sock, sbuf, strlen (sbuf), 0);
  recv (cmd_sock, rbuf, sizeof (rbuf), 0);
  printf (rbuf);
  if (CheckReplycode (rbuf, "230") != 0)
    return -1;
  memset (rbuf, 0, sizeof (rbuf));

  return 1;
}
[/PHP]
现在问题是.正常的登录的话应该显示出
230-***************************************************************
230-
230-                Welcome to ftp.intel.com!
230-
230-   For Intel product questions/comments/requests, please send mail to
230-
230-       support@mailbox.intel.com
230-
230-   For issues or problems with this FTP Server, please send mail to
230-
230-       ftp-admin@intel.com
230-
230-
230-  *************** PLEASE READ *************************
230-
230-  Use of this system by unauthorized persons or in an
230-  unauthorized manner is strictly prohibited. Unauthorized
230-  access can and will be prosecuted to the fullest extent
230-  possible.
230-
230-  Intel Internet File Exchange - One way exchange
230-  Please note that external users will only be able to
230-  download files from the /pub/outgoing area, and upload data
230-  in the /pub/incoming areas in the public exchange areas.
230-  Intel users will only be able to download from the
230-  /pub/incoming directory, and upload to /pub/outgoing.
230-
230-  **********************************************************
230-  *** Please encrypt and compress all files before       ***
230-  *** uploading them to the server.                      ***
230-  **********************************************************
230-
230-***************************************************************
230-
230 Guest login ok, access restrictions apply.

但是我的程序只显示出了第一行.不知怎么解决呢?

第一次碰socket没有任何经验....从早上一直弄到现在.
发表于 2003-7-12 20:58:06 | 显示全部楼层
葽先熟习ftp协议,去看看相应的RFC。
发表于 2003-7-12 21:02:07 | 显示全部楼层

回复: 小弟第一次编socket程序.一个ftp的login的子程序.有点问题.请教大家

[PHP]
  ......
  send (cmd_sock, sbuf, strlen (sbuf), 0);
  recv (cmd_sock, rbuf, sizeof (rbuf), 0);
  printf (rbuf);
  if (CheckReplycode (rbuf, "230") != 0)
    return -1;
  memset (rbuf, 0, sizeof (rbuf));

  return 1;
}
[/PHP]

这里你只recv、printf了一次,当然只有一行了。
 楼主| 发表于 2003-7-12 21:16:22 | 显示全部楼层
可是我单步调试的时候看了recv后.rbuf里面是有所有的内容.
但是非单步的时候就不知道了.
发表于 2003-7-12 21:30:05 | 显示全部楼层
rbuf有多大?
里面的内容是什么?特别是每一行是用'\n'还是"\r\n"结束的?
输出的情况是什么样的?
 楼主| 发表于 2003-7-12 21:41:56 | 显示全部楼层
rbuf[4096]
在recv之前已经用memset清0了.
intel返回的的字符是\x0d\x0a换行的
发表于 2003-7-12 23:12:13 | 显示全部楼层
我也写了个程序试了一下,确实如此,调试的时候一次读出了所有内容,实际运行的时候只有一行,不知道怎么搞的。
 楼主| 发表于 2003-7-14 19:24:52 | 显示全部楼层
谢谢.我已经自己搞定了.但是不知道是不是好方法.
[php]
char *
GetALine ()
{
  char aaa = ' ';
  int a = 0;
  int iLen = 0;
  do
    {
      iLen += a;
      a = recv (cmd_sock, &aaa, 1, 0);
      buffer[iLen] = aaa;
      if (aaa == '\n') break;
     }
  while (aaa != 0);
  buffer[iLen + 1] = 0;
  return buffer;
}


int
GetReply ()
{

  char reply[] = {0,0,0,0};
  memset (rbuf, 0, sizeof (rbuf));
  do
    {
      strncat (rbuf, GetALine (),sizeof(rbuf));
    }
  while (buffer[3] != 0x20);   //比较返回信息种第4个字符是不是空格.是则表明是最后一行信息.
  strncpy (reply, buffer, 3);
  return (atoi (reply));
}

int
Login ()
{

  strcpy (sbuf, "USER ");
  strcat (sbuf, user);

  strcat (sbuf, "\r\n");
  ShowMessage (sbuf);
  Command (sbuf);

  if (GetReply () != 331)
    return INVALID_SOCKET;
  ShowReMessage (rbuf);

  memset (rbuf, 0, sizeof (rbuf));
  strcpy (sbuf, "ASS ");
  strcat (sbuf, pass);
  strcat (sbuf, "\r\n");
  ShowMessage (sbuf);
  Command (sbuf);
  if (GetReply () != 230)
    return INVALID_SOCKET;
  ShowReMessage (rbuf);
  memset (rbuf, 0, sizeof (rbuf));
  return 1;
}
[/php]
发表于 2003-7-15 00:40:17 | 显示全部楼层
我是一次读入一行,用if(strstr(buf, "login ok") != NULL)判断的。
 楼主| 发表于 2003-7-15 07:19:54 | 显示全部楼层
不过在RFC959里面.ftp登录成功并不一定要返回login ok之类的字符的.

我昨天看了有关资料.觉得这里应该用非阻塞模式来写.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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