使用POST请求的简单Web服务器 - 服务器在一个或两个请求后挂起

时间:2014-01-11 17:50:34

标签: c# http tcp tcplistener

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

            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();
}

1 个答案:

答案 0 :(得分:0)

正如Yannis建议的那样,您可能无法处置您的对象,尤其是您的流。与您使用StreamReader一样,Using语句会自动为您执行此操作。例如:

  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