C#Windows服务中的FTP不会像Windows Forms应用程序中那样下载 [英] FTP in C# Windows service doesn't download like in Windows Forms app
问题描述
你好,
我编写了一个C#Windows服务,该服务从FTP下载文件.问题在于它不会下载每个文件(只有112个),但是当我从Windows窗体中朗读相同的代码时,它工作正常并下载了所有文件.
我认为问题在于它的大胆之处,我无法弄清楚为什么它在一个地方而不是在另一个地方起作用.
Hello,
I have written a C# Windows Service that downloads files from FTP. The problem is that it doesn''t download every file (only 112), but when I rum the same code from a Windows Form, it works OK and downloads all the files.
I think the problem is where it''s bold, I just can''t figure out why it works in one place and not in the other.
public string[] getFileList(string mask)
{
if (!logined)
{
login();
}
Socket cSocket = createDataSocket();
sendCommand("NLST " + mask);
if (!(retValue == 150 || retValue == 125))
{
throw new IOException(reply.Substring(4));
}
mes = "";
while (true)
{
int bytes = cSocket.Receive(buffer, buffer.Length, 0);
mes += ASCII.GetString(buffer, 0, bytes);
if (bytes < buffer.Length)
{
break;
}
}
mes = mes.Replace("\r", "");
char[] seperator = { '\n' };
string[] mess = mes.Split(seperator);
cSocket.Close();
readReply();
if (retValue != 226)
{
throw new IOException(reply.Substring(4));
}
return mess;
}
Thanks!
推荐答案
Receive
是否抛出异常?
否则,您可以更改:
IsReceive
throwing an exception?
Otherwise, you could change:
if ( bytes < buffer.Length ) break;
到
to
if ( bytes == 0 ) break;
即使有更多数据要接收,Socket.Receive
可能也无法完全填满缓冲区,但是当没有更多数据时,它将返回0.
尼克
Socket.Receive
may not totally fill your buffer even when there is more data to receive, but it will return 0 when there is no more data.
Nick
这篇关于C#Windows服务中的FTP不会像Windows Forms应用程序中那样下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!