C#Windows服务中的FTP不会像Windows Forms应用程序中那样下载 [英] FTP in C# Windows service doesn't download like in Windows Forms app

查看:87
本文介绍了C#Windows服务中的FTP不会像Windows Forms应用程序中那样下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我编写了一个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是否抛出异常?

否则,您可以更改:
Is Receive 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆