0

我正在从 tcp 流中读取数据 - 我已经使用“MLSD”命令(检索文件/目录信息)查询了 FTP 服务器。虽然由于响应大小是可变的(取决于文件/目录等的数量),但我不确定“缓冲区”应该设置多少字节。如何确保通过 tcp 流从 FTP 服务器检索所有数据?

 private string controlListener(int controlPort)
        {
            try
            {
                // Create a TcpClient. 
                // Note, for this client to work you need to have a TcpServer  
                // connected to the same address as specified by the server, port 
                // combination.
                controlClient = new TcpClient(ftpHost, controlPort);

                // Get a client stream for reading and writing. 
                controlStream = controlClient.GetStream();

                    // Because we don't know how many bytes are incoming for welcome message - we use a 2 second delay to retrieve all bytes within that time frame.
                    // Receive the TcpServer.response. 
                    // Buffer to store the response bytes.
                    Byte[] data = new Byte[4096];

                    // String to store the response ASCII representation.
                    String responseData = String.Empty;

                    // Get Control Stream Responce: Read the first batch of the TcpServer response bytes.
                    Int32 bytes = controlStream.Read(data, 0, data.Length);
                    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                    Console.WriteLine(responseData);

                    return responseData;
            }
4

3 回答 3

4

在马克的回答中......我使用这种方法,你应该很容易看到如何合并它。

    /// <summary>
    /// Read TCP response, this simple method can be re-used elsewhere as needed later.
    /// </summary>
    /// <returns></returns>
    private static string ReadResponse(NetworkStream networkStream)
    {
        // Check to see if this NetworkStream is readable.
        if (networkStream.CanRead)
        {
            var myReadBuffer = new byte[256]; // Buffer to store the response bytes.
            var completeMessage = new StringBuilder();

            // Incoming message may be larger than the buffer size.
            do
            {
                var numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                completeMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
            } while (networkStream.DataAvailable);

            return completeMessage.ToString();
        }
        return null;
    }
于 2013-01-30T04:22:29.677 回答
2

读取/接收应该在循环中使用,读取直到您获得 EOF(读取零字节)或您期望的数据量(使用框架 API)。您当前的代码无效:即使发送了 483628 个字节,单个字节也是在一次调用 Read 中返回的合法数量。

于 2012-12-11T18:55:52.680 回答
1

我用这个...

        public static byte[] readFullStream(Stream st, Int32 BufferSize)
        {
            try
            {
                Monitor.Enter(_lock);
                byte[] buffer = new byte[BufferSize];
                Int32 bytesRead;
                MemoryStream ms = new MemoryStream();
                bool finished = false;
                while (!finished)
                {
                    bytesRead = st.Read(buffer, 0, buffer.Length);
                    if (bytesRead > 0)
                    {
                        ms.Write(buffer, 0, bytesRead);
                    }
                    else
                    {
                        finished = true;
                    }
                }
                return ms.ToArray();
            }
            finally
            {
                Monitor.Exit(_lock);
            }
        }
于 2012-12-11T19:03:38.460 回答