Question

Right I have a issue with my StreamReader and StreamWriter from a network perspective, I have a network server and a client that sends message back and forth between them, this has been working great till I implemented HTTP 0.9 protocol, but with this I'm trying to change a location by using -h9 as the protocol to detect, so heres the problem.

When I send a normal name and location for example Hello everybody my StreamReader and writer send and recieve through the server correctly but if I use my protocol "-h9 hello everybody" and is changed into Put /hello everybody this becomes a problem. Below is the code.

case "-h9":// http/0.9 protocol
                            if (args.Length == 3)
                            {
                                whatIsSent = "PUT /" + args[1] + "\r\n\r\n" + args[2] ;
                            }
                            else
                            {
                                arguments = arguments.Remove(0, 4);
                                whatIsSent = "GET /" + arguments;
                            }
                            break;
if (whatIsSent == null)
                {
                    sw.WriteLine(arguments);
                }
                else
                {
                    sw.WriteLine(whatIsSent);
                }
                sw.Flush();

below is the code from my server.

data = sr.ReadLine();
                Console.WriteLine(string.Format("Received: {0}", data));
                //string data2 = sr.ReadLine();//not reading all for PUT/
                //string data3 = sr.ReadLine();
                //string Complete = data + data2 + data3;




                #region http0.9
                if (data.Contains("GET /") || data.Contains("PUT /"))
                {
                    protocol = data.Substring(0, data.IndexOf(" "));
                    data = data.Substring(1 + (protocol.Length + 1));
                    switch (protocol)
                    {
                        case "GET"://wont work cause of PUT
                            {
                                firstArg = data.Substring(0, data.Length);
                                secondArg = null;
                                useDictionary(firstArg, secondArg, ref returnData);
                                returnData = "HTTP/0.9 200 OK\r\nContent-Type: text/plain\r\n\r\n" + returnData;
                            }

                            break;
                        case "PUT"://wont work cause of PUT
                            if (data.Contains(" "))
                            {
                                firstArg = data.Substring(0, data.IndexOf(" "));
                                secondArg = data.Substring(data.IndexOf(" ") + 1, data.Length - firstArg.Length - 1);
                                useDictionary(firstArg, secondArg, ref returnData);
                            }
                            break;

As you can see I read in through data = sr.readline(); but the problem is it seems to split it up when receiving PUT instead of it being Put /hello everybody it comes through as Put /Hello" and misses the "everybody.

So I stepped through and I found out this is because it is sending Put /Hello as one line and "" as another then Everybody as the final so my data only reads in the beginning, so i thought why not use readtoend but this stops my server working at all and this is because it comes to the readtoend and just halts and doesnt move.

Could anybody help. Thanks.

No correct solution

OTHER TIPS

This line

whatIsSent = "PUT /" + args[1] + "\r\n\r\n" + args[2] ;

Sends the second argument, two CRLF's and the third argument. ReadLine() reads until the first newline character, so that seems like expected behavior to me.

ReadToEnd() reads until there is no more data to read, e.g. after the connection was closed. Since the client connection waits for the response, this won't happen.

I don't know the HTTP/0.9 protocol; but HTTP/1.0 terminates the headers section with two CRLF's and sends a Content-Length header if there is request data beyond the request headers, which tells you how many bytes to read.

(If this is the full HTTP/0.9 spec, then it seems it doesn't support request data)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top