【问题标题】:A simple web server to consume a POST request - Server hangs after one or two requests一个使用 POST 请求的简单 Web 服务器 - 服务器在一个或两个请求后挂起
【发布时间】:2014-07-11 05:13:52
【问题描述】:

我正在构建一个小型 Windows 应用程序来使用 POST 请求。下面的代码适用于 GET 请求和第一个 POST 请求。基本上,当我阅读 POST DATA 时,它第一次(或前几次)运行良好。过了一会儿(几秒钟 - 它挂起。任何传入的请求都挂起。有什么想法吗?假设内容长度是正确的。

            while (true)
            {

                System.Console.WriteLine("The server is running at port 8001...");
                System.Console.WriteLine("Waiting for a connection.....");

                TcpClient client = _listener.AcceptTcpClient();

                int incomingDataLength = client.ReceiveBufferSize;
                Stream ist = client.GetStream();

                BufferedStream bst = new BufferedStream(ist);

                int k = 0;

                String line = ReadLine(bst);
                System.Console.WriteLine(line);

                while ((line = ReadLine(bst)) != null)
                {
                    if (line == "") break;
                    System.Console.WriteLine(line);
                }

                MemoryStream ms = new MemoryStream();
                int contentLen = 3429;
                //if (this.HttpHeaders.ContainsKey("Content-Length"))
                {
                    //content_len = Convert.ToInt32(this.HttpHeaders["Content-Length"]);
                    byte[] buf = new byte[4096];
                    int to_read = content_len;
                    while (to_read > 0)
                    {
                        int numread = bst.Read(buf, 0, Math.Min(buf.Length, to_read));
                        if (numread == 0)
                        {
                            if (to_read == 0) break;
                            else throw new Exception("client disconnected during post");
                        }
                        to_read -= numread;
                        ms.Write(buf, 0, numread);
                    }
                    ms.Seek(0, SeekOrigin.Begin);
                }

                using (StreamReader sr = new StreamReader(ms))
                {
                    System.Console.WriteLine(sr.ReadToEnd());
                }

                bst.Close();
                client.Close();

ReadLine 是

private String ReadLine(Stream stream)
{
    int k;
    StringBuilder lineBuilder = new StringBuilder();
    while (true)
    {
        k = stream.ReadByte();
        if (k < 0) continue;

        char c = Convert.ToChar(k);
        if (c == '\n') break;
        if (c == '\r') continue;

        lineBuilder.Append(c);
    }

    return lineBuilder.ToString();
}

【问题讨论】:

  • 什么是ReadLine?暂停调试器时它挂在哪里?对 bst 和客户端使用 using 块。
  • 不用担心使用块。它们不相关。这段代码有一百万件事情可能会更好,但我对它为什么会阻塞很感兴趣。
  • 它在 ReadLine 中阻塞
  • 无法复制。 postimg.org/image/7tqcsth3z我错过了什么?你没有包括什么?你的测试客户是什么?看起来它不再发送任何数据。坏了。
  • 测试客户端是高级 REST 客户端(Chrome 扩展)。尝试运行 POST 请求。等待几分钟,然后再次运行。

标签: c# http tcp tcplistener


【解决方案1】:

正如Yannis 建议的那样,您可能不会处理您的对象,尤其是您的流。 Using 语句,就像您对 StreamReader 所做的那样,将自动为您执行此操作。例如:

  while (true)
  {

  System.Console.WriteLine("The server is running at port 8001...");
  System.Console.WriteLine("Waiting for a connection.....");

  using (TcpClient client = _listener.AcceptTcpClient())
  {
        int incomingDataLength = client.ReceiveBufferSize;

        using (Stream ist = client.GetStream())
        {
              //Stream ist = client.GetStream();
              using (BufferedStream bst = new BufferedStream(ist))
              {
                     //BufferedStream bst = new BufferedStream(ist);

                     int k = 0;

                     String line = ReadLine(bst);
                     System.Console.WriteLine(line);

                      while ((line = ReadLine(bst)) != null)
                      {
                            if (line == "") break;
                            System.Console.WriteLine(line);
                      }

                      using (MemoryStream ms = new MemoryStream())
                      {
                            //MemoryStream ms = new MemoryStream();
                            int contentLen = 3429;
                            if (this.HttpHeaders.ContainsKey("Content-Length"))
                            {
                                  //content_len = Convert.ToInt32(this.HttpHeaders["Content-Length"]);
                                  byte[] buf = new byte[4096];
                                  int to_read = content_len;
                                  while (to_read > 0)
                                  {
                                         int numread = bst.Read(buf, 0, Math.Min(buf.Length, to_read));
                                         if (numread == 0)
                                         {
                                               if (to_read == 0) break;
                                              else throw new Exception("client disconnected during post");
                                         }
                                         to_read -= numread;
                                         ms.Write(buf, 0, numread);
                                   }
                                   ms.Seek(0, SeekOrigin.Begin);
                             }

                       using (StreamReader sr = new StreamReader(ms))
                       {
                             System.Console.WriteLine(sr.ReadToEnd());
                       }

                       //bst.Close();
                       client.Close();
                 } //end memorystream
              } //end bufferedsteam
          } //end stream
      } //end tcpClient
  } //end while

【讨论】:

    猜你喜欢
    • 2011-06-01
    • 1970-01-01
    • 2018-09-29
    • 2013-04-19
    • 2014-12-05
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多