我开发一个FTP的搜索器,用FTP协议中的LIST命令能收到信息返回的信息,但是有的文件列表内容没有收到,这是为什么?我用了同步,异步都试了,还是不行,哪位遇到过我这样的问题,或者是有解决的办法,请跟帖,不胜感激。
源代码如下(异步)
if(nws.CanRead)
{
allDone.Reset();
nws.BeginRead(buffer,0,buffer.Length,new AsyncCallback  (ReadCallBack),nws);
allDone.WaitOne();
}异步调用代码
private void  ReadCallBack(IAsyncResult ar)
{
NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
int readerLength=myNetworkStream.EndRead(ar);
         message +=Encoding.Default.GetString(buffer, 0, readerLength);
}

解决方案 »

  1.   

    用"NLST"试了,也不能,还是出现一样的问题。
      

  2.   

    你帖的代码和list命令无关,ftp有二个连接
      一个传输控制,一个数据连接,传输控制在21端口, 
      数据连接又分二种模式,PORT主动和PASV被动连接
      LIST 需要数据连接。 
      

  3.   

    我觉得不是这个问题,我使用被动模式,这是我的完整代码。
    public string[] GetFileList(string path) 
    {
    if(!logined)
    {
    Login();
    }
    NetworkStream nws=CreateNetworkStream();
    SendCommand("LIST "+path);
    retValue=GetRetValue();
    if(!(retValue==150||retValue==125||retValue==226))
    {
    throw new IOException(message.Substring(4));
    }
    message="";
    /*while(true)
    {
    int bytes=nws.Read(buffer,0,buffer.Length);
    message+=Encoding.Default.GetString(buffer,0,bytes);
    if(bytes<buffer.Length)
    {
    break;
    }
    }*/
    if(nws.CanRead)
    {
    allDone.Reset();
    nws.BeginRead(buffer,0,buffer.Length,new AsyncCallback(ReadCallBack),nws);
    allDone.WaitOne();
    }
    else
    {
    Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
    }
    char seperator='\n';
    string[] dealStr=message.Split(seperator);
    nws.Close();
    //ReadMessage();
    //GetRetValue();
    //if(retValue != 226)
    //{
    // throw new IOException(message.Substring(4));
    //}
    return dealStr;
    }
      

  4.   

    需要这样。
    list的结果是从数据连接返回的。用port模式,需建立socket侦听,将用port命令将ip地址与侦听端口发送给服务器。
    accept后就可以开始使用此socket进行数据接收了。