【问题标题】:how to detect disconnection to finish sending data trough tcpclient如何检测断开连接以完成通过 tcpclient 发送数据
【发布时间】:2011-07-13 17:09:12
【问题描述】:

我需要使用此代码通过 tcpclient 发送错误列表

private bool TryConn(out TcpClient cliente)
{
    var client = new TcpClient();
    try
    {
        client.Connect(IpAddress, PortNumber);
        cliente = client;
        return true;
    }
    catch
    {
        cliente = null;
        return false; 
    }

}
public void ProcesssRecovery()
{
    //NonMassiveErrorerror= new NonMassiveError();
    TcpClient client;
    //get error
    IEnumerable<NonMassiveError> errorNotNotified = GetUncheckedNonMassiveError();

    //check if lista is not empty 
    if (errorNotNotified .Count() >0 )
    {
        // check connection
        if (TryConn(out client))
        {
            foreach (NonMassiveError error in errorNotNotified )
            {       // sending error<--how detect conn stops
                SendMessage(error, client, "asin" , "");
                error.Save();
            }

        }

    }
        //stop thread 10mins
    else
    {
        Thread.Sleep(TimeSpan.FromMinutes(10)); 
    }           
}

}

如何检查foreach 中的连接是否断开以停止发送数据?

【问题讨论】:

    标签: c# networking tcpclient


    【解决方案1】:

    您应该稍等片刻,然后阅读对您发送的数据的响应。 如果服务器确认,则可以(响应 = 0),如果不停止发送。

    【讨论】:

      【解决方案2】:

      SendMessage() 是你自己的方法,对吧?检查其中的 socket.Send() 是否返回 0 字节。这意味着套接字已断开连接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 1970-01-01
        • 1970-01-01
        • 2012-08-05
        • 2013-06-09
        • 2014-04-08
        相关资源
        最近更新 更多