2014-01-11 76 views
1

我正在建立一個小窗口應用程序消耗POST請求。下面的代碼適用於GET請求和第一個POST請求。基本上,當我讀取POST數據時,它第一次(或前幾次)工作正常。 。?它掛任何傳入請求掛起任何想法假設內容長度是正確的一個簡單的Web服務器消耗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(); 
} 
+0

什麼是'ReadLin e'?暫停調試器時,它會在哪裏掛起?爲bst和客戶端使用'使用'塊。 – usr

+0

不要擔心使用塊。它們不相關。有一百萬件事情可以對此代碼更好,但我對它爲什麼會阻塞感興趣。 – Yannis

+0

它在ReadLine中阻止 – Yannis

回答

0

由於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 
相關問題