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;
                }
            }

Recommended Answers

All 4 Replies

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.

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);

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();
      }
    }

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.