gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:54:37 28 4
gpt4 key购买 nike

我正在构建一个小型 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();
}

最佳答案

作为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

关于c# - 使用 POST 请求的简单 Web 服务器 - 服务器在一两个请求后挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21065734/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com