I am writing a TCP Client and a Server in C# which use manually written HTTP requests to communicate with each other. The trouble I have is with reading from the Network Stream
using StreamReader
. So far, I have tried many methods but to no avail.
The requests I get from my TCP Client are in in various forms. For updating database, the requests look like this (CRLF is a constant I use to denote "\r\n"
string):
HTTP 1.0:
"POST /" + name + " HTTP/1.0" + CRLF + "Content-Length: " + length + CRLF + CRLF + location;
HTTP 1.1:
"POST / HTTP/1.1" + CRLF + hostname + "Content-Length: " + length + CRLF + CRLF + nameLocString;
The requests are of correct form and the client is sending them correctly - I have tested this on a server to which I have access that responded to them without problems.
The problems I have are with my TCP Listener code. To avoid posting the whole code, I will just include the parts of the code that are problematic (found out by debugging).
Server code:
NetworkStream socketStream = new NetworkStream(connection);
StreamReader sr = new StreamReader(socketStream);
string input = ReadAllLinesWithNull(sr); // reading version 1
string input = ReadAllLinesWithEndOfStream(sr); // reading version 2
string input = ReadAllLinesWithPeek(sr); // reading version 3
string input = sr.ReadToEnd(); // reading version 4
And the methods used are:
static string ReadAllLinesWithNull(StreamReader sr)
{
string input;
string nextLine;
input = sr.ReadLine();
while ((nextLine = sr.ReadLine()) != null)
{
Console.WriteLine(input);
input += nextLine;
}
sr.Close();
return input;
}
static string ReadAllLinesWithEndOfStream(StreamReader sr)
{
string input = "";
while (!sr.EndOfStream)
{
input += sr.ReadLine();
}
sr.Close();
return input;
}
static string ReadAllLinesWithPeek(StreamReader sr)
{
string input = "";
while (sr.Peek() >= 0)
{
input += sr.ReadLine();
}
sr.Close();
return input;
}
None of these methods for reading worked. With my connection timeouts set, I have been getting IO Exception that it took too long to read/the connection was forcibly closed. I switched off the timeouts and the Read took indefinite amounts of time.
Thanks to using ReadLine()
s I was able to single out the place where it ultimately hangs for all versions of protocol and found out that when there is cluster of two CRLFs ("\r\n\r\n"
), the Stream Reader is not able to cope with this and gets stuck.
Do you have any suggestions as on how to get around this? I need to use the version with multiple CRLFs as it is in the specification.
If you need any additional information, I will try to supply it as sson as possible.