我正在从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;
}
缓冲的要点是,如果你做得很好(你做得不对),你会成功读取整个流,而不管缓冲区大小。缓冲区只会影响使用的内存量,并可能影响应用程序的性能。 – Servy