开发者

how to detect disconnection to finish sending data trough tcpclient

开发者 https://www.devze.com 2023-03-20 14:29 出处:网络
i need to send a list of errors trough tcpclient with this code private bool TryConn(out TcpClient cliente)

i need to send a list of errors trough tcpclient with this code

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))
        开发者_JAVA百科{
            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)); 
    }           
}

}

How can i check if the connection falls down in the foreach to stop sending data??


You should wait a little bit and read the response to the data you sent. If the server acknowledge then it's ok(Response = 0), if not stop sending.


SendMessage() is your own method, right? Check if socket.Send() in it returns 0 bytes. That means that the socket have gotten disconnected.

0

精彩评论

暂无评论...
验证码 换一张
取 消