【问题标题】:TCP Client - set timeout C# CF 3.5TCP 客户端 - 设置超时 C# CF 3.5
【发布时间】:2014-02-18 16:55:58
【问题描述】:

我有 Windows CE 6.0 CF 3.5 的移动射频终端,我需要尽快将数据发送到我的服务器。 问题是wifi连接在使用时不够稳定。 当连接因异常中止时,我通过重复发送减少了数据丢失。但例外时间约为 21 秒,对我来说太长了。重复连接成功(大部分),但我需要缩短异常时间,或者找到不同的连接方法。

对于数据接收,我在终端中有 TCP 服务器。在这里,我有一个带计时器的线程,超时后我关闭客户端(TcpClient)并从中捕获异常:-)。但我的意思是这种方法并不理想。

有什么想法吗?? 谢谢!

下面是我的 TCP 客户端连接代码

    internal string TCPsend(string strMessage)
    {
        string exception = string.Empty;
        TcpClient client = null;
        try
        {
            client = new TcpClient(IPServer, PortServer);
            client.SendTimeout = 500;                        //no effect
            Stream stream = client.GetStream();
            StreamWriter strWriter = new StreamWriter(stream);
            strWriter.Write(strMessage);
            strWriter.Close();
        }
        catch (Exception ex)
        {
            exception = ex.ToString();
            utility.SaveExceptionLog("SendTCP A: " + ex.ToString());
        }
        finally
        {
            if (client != null)
            {
                client.Close();
            }
        }
        return exception;
    }

【问题讨论】:

  • 在您发布的代码中,大部分时间将花费在打开和关闭连接上,保持连接打开并使用周期性的心跳消息来检测网络中断的效率要高得多在没有其他流量的情况下这样做。

标签: c# compact-framework tcpclient connection-timeout


【解决方案1】:

如果您想进行连接检查,您可能应该打开新线程,在其中循环检查已建立的连接。

这里是讨论琐碎检查的答案:What is the best way to check for Internet connectivity using .NET?

这会影响整体发送性能,但应该会减少等待时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2011-08-08
    • 2020-12-07
    • 2019-10-05
    • 2011-12-24
    • 2011-07-11
    相关资源
    最近更新 更多