Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 套接字读取前Thread.Sleep()的替代方法_C#_Winforms_Sockets_Ftp_Network Programming - Fatal编程技术网

C# 套接字读取前Thread.Sleep()的替代方法

C# 套接字读取前Thread.Sleep()的替代方法,c#,winforms,sockets,ftp,network-programming,C#,Winforms,Sockets,Ftp,Network Programming,我正在使用此FtpClient库从WinForms应用程序连接到大型机。我正在使用thread.Sleep,让线程在开始读取之前等待响应,否则它会冻结。有没有其他方法可以做到这一点 public void Login() { if (this.loggedin) this.Close(); Debug.WriteLine("Opening connection to " + this.server, "FtpClient"); IPAddress addr = nu

我正在使用此FtpClient库从WinForms应用程序连接到大型机。我正在使用thread.Sleep,让线程在开始读取之前等待响应,否则它会冻结。有没有其他方法可以做到这一点

public void Login() 
{
    if (this.loggedin) this.Close();

    Debug.WriteLine("Opening connection to " + this.server, "FtpClient");

    IPAddress addr = null;
    IPEndPoint ep = null;

    try
    {
        this.clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        addr = Dns.Resolve(this.server).AddressList[0];
        ep = new IPEndPoint(addr, this.port);
        this.clientSocket.Connect(ep);
    }
    catch (Exception ex)
    {
        // doubtfull
        if (this.clientSocket != null && this.clientSocket.Connected) this.clientSocket.Close();

        throw new FtpException("Couldn't connect to remote server", ex);
    }

    **Thread.Sleep(4000);**
    this.readResponse();
    ...
}

private void readResponse()
{
    this.message = "";
    this.result = this.readLine();

    if (this.result.Length > 3)
        this.resultCode = int.Parse(this.result.Substring(0, 3));
    else
        this.result = null;
}

private string readLine()
{
    while (true)
    {
        this.bytes = clientSocket.Receive(this.buffer, this.buffer.Length, 0);
        this.message += ASCII.GetString(this.buffer, 0, this.bytes);

        if (this.bytes < this.buffer.Length) break;
    }

    string[] msg = this.message.Split('\n');
    if (this.message.Length > 2)
    {
        this.message = msg[msg.Length - 2];
        try { response = msg[msg.Length - 3]; }
        catch { }
    }
    else
    {
        this.message = msg[0];
    }

    if (this.message.Length > 4 && !this.message.Substring(3, 1).Equals(" ")) return this.readLine();

    if (this.verboseDebugging)
    {
        for (int i = 0; i < msg.Length - 1; i++)
        {
            Debug.Write(msg[i], "FtpClient");
        }
    }
    return message;
}

public void sendCommand(String command)
{
    if (this.verboseDebugging) Debug.WriteLine(command, "FtpClient");

    Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
    clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
    this.readResponse();
}
public void Login()
{
如果(this.loggedin)this.Close();
Debug.WriteLine(“打开到”+this.server,“FtpClient”的连接);
IPAddress addr=null;
IPEndPoint ep=null;
尝试
{
this.clientSocket=新套接字(AddressFamily.InterNetwork、SocketType.Stream、ProtocolType.Tcp);
addr=Dns.Resolve(this.server).AddressList[0];
ep=新的IPEndPoint(地址,此端口);
这个.clientSocket.Connect(ep);
}
捕获(例外情况除外)
{
//毫无疑问
如果(this.clientSocket!=null&&this.clientSocket.Connected)this.clientSocket.Close();
抛出新的FtpException(“无法连接到远程服务器”,例如);
}
**睡眠(4000)**
这是readResponse();
...
}
私有void readResponse()
{
this.message=“”;
this.result=this.readLine();
如果(this.result.Length>3)
this.resultCode=int.Parse(this.result.Substring(0,3));
其他的
this.result=null;
}
私有字符串读取行()
{
while(true)
{
this.bytes=clientSocket.Receive(this.buffer,this.buffer.Length,0);
this.message+=ASCII.GetString(this.buffer,0,this.bytes);
如果(this.bytes2)
{
this.message=msg[msg.Length-2];
试试{response=msg[msg.Length-3];}
捕获{}
}
其他的
{
this.message=msg[0];
}
如果(this.message.Length>4&&!this.message.Substring(3,1).Equals(“”))返回this.readLine();
if(this.verboseDebugging)
{
对于(int i=0;i
是的,如果您可以使用.NET 4.0,您就可以使用任务API

您可以通过阅读本文了解更多信息:


它为您提供了一种创建流程的方法,在该流程中,任务将等待上一个任务结束。看一看

使用异步编程模型:

socket.BeginConnect(ep, new AsyncCallback(Connected), socket);

void Connected (IAsyncResult result)
{
    var socket = (Socket)result.AsyncState;

    // do the stuff

    socket.EndConnect(result);
}
编写
Thread.Sleep()
确实是一种糟糕的做法

如果您实际上正在使用FTP客户端,请使用


并将
readResonse
作为回调传递。它将在响应准备就绪时准确调用

坏习惯?我想不是。但是,是的,还有其他方法可以奏效。好吧,假设这是一个糟糕设计的证明,那么我不同意那篇文章。这是关于为什么使用thread.sleep()的推测。在我的书中,睡一根线是一种很好的表达方式,“我无事可做,请在我再试之前处理一些事情”。还是。我认为一个良好的睡眠是在循环的时候,你实际上需要等待一个结果,这是在浪费CPU时间。我认为异步调用是一种更好的设计。它可以用于登录,但在发送其他命令(上载/下载)时,我遇到了这样一个错误:“当先前的异步调用正在进行时,无法阻止此套接字上的调用。”@user558138:抱歉,忘了说您需要调用
EndConnect()
我仍然遇到同样的错误。发送(cmdBytes,cmdBytes.Length,0)//关于这个line@user558138:您将
发送的
放在哪里?。也请查看