我正在从 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;
}