在网上下载了一个他人写的简单的 httpserver , 其中有一个部分代码是 响应post请求,然后发送回去对应的响应结果
响应的部分代码如下
其中 socket 定义的是 public TcpClient socket;现在碰到的问题是由于响应处理的时间太长,导致处理完成后,最后发送回去响应信息时,程序运行到 outputStream.Flush(); 就提示 ”unable to write data to the transport connection:远程主机强迫关闭了一个现有连接。"我想这个原因应该是 tcp/ip 连接断开了,我想问一下,哪里可以把 tcp/ip 连接的超时时间设置的长一些呢?大神给点思路,谢谢!
public void process() {
// we can't use a StreamReader for input, because it buffers up extra data on us inside it's
// "processed" view of the world, and we want the data raw after the headers
inputStream = new BufferedStream(socket.GetStream()); // we probably shouldn't be using a streamwriter for all output from handlers either
outputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
try {
parseRequest();
readHeaders();
if (http_method.Equals("GET")) {
handleGETRequest();
} else if (http_method.Equals("POST")) {
handlePOSTRequest();
}
} catch (Exception e) {
Console.WriteLine("Exception: " + e.ToString());
writeFailure();
}
outputStream.Flush();
// bs.Flush(); // flush any remaining output
inputStream = null; outputStream = null; // bs = null;
socket.Close();
}
响应的部分代码如下
其中 socket 定义的是 public TcpClient socket;现在碰到的问题是由于响应处理的时间太长,导致处理完成后,最后发送回去响应信息时,程序运行到 outputStream.Flush(); 就提示 ”unable to write data to the transport connection:远程主机强迫关闭了一个现有连接。"我想这个原因应该是 tcp/ip 连接断开了,我想问一下,哪里可以把 tcp/ip 连接的超时时间设置的长一些呢?大神给点思路,谢谢!
public void process() {
// we can't use a StreamReader for input, because it buffers up extra data on us inside it's
// "processed" view of the world, and we want the data raw after the headers
inputStream = new BufferedStream(socket.GetStream()); // we probably shouldn't be using a streamwriter for all output from handlers either
outputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
try {
parseRequest();
readHeaders();
if (http_method.Equals("GET")) {
handleGETRequest();
} else if (http_method.Equals("POST")) {
handlePOSTRequest();
}
} catch (Exception e) {
Console.WriteLine("Exception: " + e.ToString());
writeFailure();
}
outputStream.Flush();
// bs.Flush(); // flush any remaining output
inputStream = null; outputStream = null; // bs = null;
socket.Close();
}
这个断开是客户端http设置的,一段时间没收到回应,客户端自己断开了
响应用了 50s 。
本来可以不用这么久,因为要进行一些硬件操作。怕硬件操作时间不够,所以多预留了一些时间。
然后就发现这个时候 tcp/ip 断开了连接了。
如果是客户端自己断掉了的话,那即使改成其他方式做 httpserver 也是没有什么帮助了。