Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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# 网络流读取超时超过1000毫秒_C#_Sockets_Tcp_Networkstream - Fatal编程技术网

C# 网络流读取超时超过1000毫秒

C# 网络流读取超时超过1000毫秒,c#,sockets,tcp,networkstream,C#,Sockets,Tcp,Networkstream,如果超时设置超过1000毫秒,我似乎无法使networkstream获得读取超时异常。这有意义吗 private void startListening() { while (!_shouldStop) { _terminate = false; try { sendMessageToParent(String.Format("Waiti

如果超时设置超过1000毫秒,我似乎无法使networkstream获得读取超时异常。这有意义吗

        private void startListening()
    {
        while (!_shouldStop)
        {
            _terminate = false;
            try
            {
                sendMessageToParent(String.Format("Waiting for a connection on {0}:{1}",_IP,_PORT));
                Program.sendNotifyIconMessage("MediaSync - Started");
                TcpClient c = l.AcceptTcpClient();


                sendMessageToParent("Client connected...");
                ns = c.GetStream();
                ns.ReadTimeout = 5000; 
              //  ns.WriteTimeout = 10000;

                while (!_shouldStop && !_terminate)
                {
                    try
                    {
                        getHeader2();
        //                getHeader();
                    }
                    catch (Exception e)
                    {
                        sendMessageToParent(e.Message);
                        sendMessageToParent("Exception: " + e.Message,true);
                        _terminate = true;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                sendMessageToParent("Stopped");
                sendMessageToParent("Exception: " + e.Message,true);
                return;
            }
        } 
    }

    private void getHeader2()
    {
        bool headerComplete = false;
        int headerBytesRead = 0;
        //header = new MyHeader();
        Stopwatch stopWatch = new Stopwatch();
        while (!headerComplete)
        {
            stopWatch.Start();
            if (headerOffset > 0)
            {
                headerString += System.Text.Encoding.ASCII.GetString(buffer, headerOffset, b - headerOffset);
                // this was how much was read from the offset
                headerBytesRead += b - headerOffset;
                headerOffset = 0;
            }

            header = new MyHeader(headerString.Split(',').Select(t => t.Split('=')).ToArray());

            if (!header.isHeaderComplete())
            {
                b = ns.Read(buffer, 0, buffer.Length);
                headerBytesRead += b;
                if (b < 1)
                {
                    stopWatch.Stop();
                    TimeSpan ts = stopWatch.Elapsed;
                    if (ts.Seconds >= 5)
                    {
                        sendMessageToParent("Past limit");
                        ns.Close();
                    }
                    else
                    {
                        stopWatch.Start();
                    }
                }
                else
                {
                    stopWatch.Reset();
                }
                headerString += System.Text.Encoding.ASCII.GetString(buffer, 0, b);
            }
            else
            {
                contentOffset = b - (headerBytesRead - header.HeaderLength);
                headerComplete = true;
            }
        }
        sendMessageToParent(String.Format("Received header data: {0}", header.HeaderData));
        processAction();
    }
使现代化
看起来5000毫秒是一个神奇的数字,它可以工作到4000毫秒。是否有某种我不知道的内在限制

更新2
好的,我更新了代码以显示我在做什么。我想我已经找到了一个解决办法,使用秒表检查在预期时间内是否没有读取数据。它可以工作,但我仍然想知道超时是否真的有限制器。其中一个答案表明了这一点。有人能确认吗?

5000毫秒是一个神奇的数字,它最多可以工作4000毫秒?在代码片段中,您会显示许多不相关的代码,但不会显示您阅读的代码。你能发布一个简明的代码片段吗?我已经添加了所需的信息。我也有一个解决方案,但我想验证的主要问题是为什么超时不起作用。@usr这信息足够了吗?如果你发布一个5行可执行程序,我会研究这个问题,因为这是一个有趣的问题。目前,这将是一件琐事,我不想这样做: