954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using StreamReader with sockets for the 2nd time

Hi everyone,

I have a problem with reading a socket for the 2nd time. I'll explain.
By using TcpClient I set up a connection to a webserver. Then I perform a GET request and read it out. When I do another GET request afterwards, that works, but I can't read the socket.

Somebody pls help me out.
If you find other small thingies in the code that are wrong you can tell me too. I'm just a beginner in C#.

Thanks in advance!

TcpClient oTcpClient = new TcpClient();
            IAsyncResult oIAsyncResult = oTcpClient.BeginConnect("www.example.com", 80, null, null);
            bool success = oIAsyncResult.AsyncWaitHandle.WaitOne(5000, true);
            if (!success)
            {
                oTcpClient.Close();
                Console.WriteLine("Timeout...");
                return;
            }
            oTcpClient.EndConnect(oIAsyncResult);

            NetworkStream oNetworkStream = oTcpClient.GetStream();
            StreamReader oStreamReader = new StreamReader(oNetworkStream);
            StreamWriter oStreamWriter = new StreamWriter(oNetworkStream);
            oStreamWriter.AutoFlush = true;

oStreamWriter.Write("GET /index.php HTTP/1.1\r\n"
                + "Host: www.example.com\r\n"
                + "User-Agent: Mozilla/5.0\r\n"
                + "Connection: close\r\n"
                + "\r\n");

            string output = "";
            string src = "";
            while (output != null)
            {
                output = oStreamReader.ReadLine();
                if (output != null)
                {
                    src += output;
                }
            }

//here I do stuff with the src

oStreamWriter.Write("GET /index.php HTTP/1.1\r\n"
                + "Host: www.example.com\r\n"
                + "User-Agent: Mozilla/5.0\r\n"
                + "Connection: close\r\n"
                + "\r\n");
//starting from here it goes wrong
            string output = "";
            string src = "";
            while (output != null)
            {
                output = oStreamReader.ReadLine();
                if (output != null)
                {
                    src += output;
                }
            }
fulcrum9
Newbie Poster
3 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

I dont think you can do this. Whenever you read some thing from stream you actually move the reader's position in the Stream. This will happen even if you use StreamReader. So if you have read the stream till end, reader's position is in end. Now if you want to read the stream again you have to set the reader position using seek() method of stream.
You can't call seek method in NetworkStream. As this method is not currently supported and always throws a NotSupportedException.

DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

I didn't find any problem in your code:

TcpClient oTcpClient = new TcpClient();
IAsyncResult oIAsyncResult = oTcpClient.BeginConnect("www.daniweb.com", 80, null, null);
bool success = oIAsyncResult.AsyncWaitHandle.WaitOne(5000, true);
       if (!success)
        {
            oTcpClient.Close();
            Console.WriteLine("Timeout...");
            return;
        }
        oTcpClient.EndConnect(oIAsyncResult);

        NetworkStream oNetworkStream = oTcpClient.GetStream();
        StreamReader oStreamReader = new StreamReader(oNetworkStream);
        StreamWriter oStreamWriter = new StreamWriter(oNetworkStream);
        oStreamWriter.AutoFlush = true;

        oStreamWriter.Write("GET /forums/thread208224.html HTTP/1.1\r\n"
                        + "Host: www.daniweb.com\r\n"
                        + "User-Agent: Mozilla/5.0\r\n"
                        + "Connection: close\r\n"
                        + "\r\n");

        string output = "";
        string src = "";
        while (output != null)
        {
            output = oStreamReader.ReadLine();
            if (output != null)
            {
                src += output;
            }
        }
     Console.WriteLine(src);
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

fulcrum -- You may also consider using HttpWebRequest to simplify what you are trying to accomplish. I realize this isn't what you asked but I thought I would mention it in case you haven't seen it before.

private static void GetPage(string URL, string Referrer)
    {
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URL);
      req.Method = "GET";
      req.Referer = Referrer;
      //req.Headers.Add

      HttpWebResponse objResponse = (HttpWebResponse)req.GetResponse();
      using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
      {
        string htmlStuff = sr.ReadToEnd();
        sr.Close();
      }
    }
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

@adatapost: If you only want to send&read data once, the code is fine. Otherwise not. That's the problem here. :P

@sknake: I know I can use HttpWebRequest, but I wanted to use sockets because then I'd already know how to use sockets. And I already knew how to build GET request. :) But I might try it out if we can't find a solution for the socket read problem.

@DangerDev: You gave me the best answer so far. Thx :D I'll search on google about that seek function. Maybe somebody else found a solution for it.

Thanks everybody for the replies so far. I appreciate the help.

fulcrum9
Newbie Poster
3 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You