lucycopp
lucycopp

Reputation: 213

C# Server will not read in multiple lines sent from the client

I am currently creating a client which communicates with a server. Up until now, any communication has been no longer than a line, so I've been able to user Streamreader readline to get the clients input. However, I now have commands which are multiple lines of code, but the server will only read in the first line as I have used readline(). I have tried readtoend(), however when I do this, the connection between the server and client fails for some reason.

The following is the code in my client, an example which shows a command which consists of multiple lines being sent:

  else if (args.Length == 1)
                {
                    sw.WriteLine("GET" + " /?name=" + args[0] + " HTTP/1.1");
                    sw.WriteLine("HOST: " + host);
                    sw.WriteLine("");
                    sw.Flush();
                }

And here is the code in my server, which reads in the line:

 string line = sr.ReadToEnd().Trim();
        Console.WriteLine("Respond Recieved " + line);
        String[] Sections = line.Split(new char[] { ' ' }, 2);

So, to help clarify the problem, if I sent the command as shown above to the server, the response from the server would be "Response Received: GET" + " /?name=" + args[0] + " HTTP/1.1" and not the other lines.

Any help would be greatly appreciated!

Thank you

Lucy

Upvotes: 0

Views: 915

Answers (3)

Muhammad Saqib
Muhammad Saqib

Reputation: 41

You can read all of the data

 using (StreamReader sr = new StreamReader(filePath))
  {
    string line;
       while ((line = sr.ReadLine()) != null)
        {
            Console.WriteLine(line);
            //Or do your work here.
        }
  }

Upvotes: 0

GeirGrusom
GeirGrusom

Reputation: 1019

ReadToEnd() may block indefinitely on non-closing streams like NetworkStream. I would guess that the error you get is a timeout or something similar?

The easiest way to read the headers is to just call ReadLine() multiple times. If it returns null that means end of stream (client disconnected, probably an error condition unless Connection: Closed was specified and you have read an empty line). An empty line means that you reached the end of the headers. After that comes the optional request body which would be specified by the optional Content-Length header.

Keep in mind that StreamReader buffers data! This is important if the content body is anything other than ASCII text.

Generally, I don't think you should implement HTTP reading yourself, but it's an interesting learning experience into an important standard.

Upvotes: 1

ddeamaral
ddeamaral

Reputation: 1443

Can you try the following code I got from this link?

            using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }

Upvotes: 0

Related Questions