Using StreamReader with sockets for the 2nd time

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 3
Reputation: fulcrum9 is an unknown quantity at this point 
Solved Threads: 0
fulcrum9 fulcrum9 is offline Offline
Newbie Poster

Using StreamReader with sockets for the 2nd time

 
0
  #1
Jul 31st, 2009
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!

  1. TcpClient oTcpClient = new TcpClient();
  2. IAsyncResult oIAsyncResult = oTcpClient.BeginConnect("www.example.com", 80, null, null);
  3. bool success = oIAsyncResult.AsyncWaitHandle.WaitOne(5000, true);
  4. if (!success)
  5. {
  6. oTcpClient.Close();
  7. Console.WriteLine("Timeout...");
  8. return;
  9. }
  10. oTcpClient.EndConnect(oIAsyncResult);
  11.  
  12. NetworkStream oNetworkStream = oTcpClient.GetStream();
  13. StreamReader oStreamReader = new StreamReader(oNetworkStream);
  14. StreamWriter oStreamWriter = new StreamWriter(oNetworkStream);
  15. oStreamWriter.AutoFlush = true;
  16.  
  17. oStreamWriter.Write("GET /index.php HTTP/1.1\r\n"
  18. + "Host: www.example.com\r\n"
  19. + "User-Agent: Mozilla/5.0\r\n"
  20. + "Connection: close\r\n"
  21. + "\r\n");
  22.  
  23. string output = "";
  24. string src = "";
  25. while (output != null)
  26. {
  27. output = oStreamReader.ReadLine();
  28. if (output != null)
  29. {
  30. src += output;
  31. }
  32. }
  33.  
  34. //here I do stuff with the src
  35.  
  36. oStreamWriter.Write("GET /index.php HTTP/1.1\r\n"
  37. + "Host: www.example.com\r\n"
  38. + "User-Agent: Mozilla/5.0\r\n"
  39. + "Connection: close\r\n"
  40. + "\r\n");
  41. //starting from here it goes wrong
  42. string output = "";
  43. string src = "";
  44. while (output != null)
  45. {
  46. output = oStreamReader.ReadLine();
  47. if (output != null)
  48. {
  49. src += output;
  50. }
  51. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Using StreamReader with sockets for the 2nd time

 
0
  #2
Jul 31st, 2009
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.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Using StreamReader with sockets for the 2nd time

 
0
  #3
Jul 31st, 2009
I didn't find any problem in your code:
  1. TcpClient oTcpClient = new TcpClient();
  2. IAsyncResult oIAsyncResult = oTcpClient.BeginConnect("www.daniweb.com", 80, null, null);
  3. bool success = oIAsyncResult.AsyncWaitHandle.WaitOne(5000, true);
  4. if (!success)
  5. {
  6. oTcpClient.Close();
  7. Console.WriteLine("Timeout...");
  8. return;
  9. }
  10. oTcpClient.EndConnect(oIAsyncResult);
  11.  
  12. NetworkStream oNetworkStream = oTcpClient.GetStream();
  13. StreamReader oStreamReader = new StreamReader(oNetworkStream);
  14. StreamWriter oStreamWriter = new StreamWriter(oNetworkStream);
  15. oStreamWriter.AutoFlush = true;
  16.  
  17. oStreamWriter.Write("GET /forums/thread208224.html HTTP/1.1\r\n"
  18. + "Host: www.daniweb.com\r\n"
  19. + "User-Agent: Mozilla/5.0\r\n"
  20. + "Connection: close\r\n"
  21. + "\r\n");
  22.  
  23. string output = "";
  24. string src = "";
  25. while (output != null)
  26. {
  27. output = oStreamReader.ReadLine();
  28. if (output != null)
  29. {
  30. src += output;
  31. }
  32. }
  33. Console.WriteLine(src);
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,442
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 624
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Using StreamReader with sockets for the 2nd time

 
0
  #4
Jul 31st, 2009
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.

  1. private static void GetPage(string URL, string Referrer)
  2. {
  3. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URL);
  4. req.Method = "GET";
  5. req.Referer = Referrer;
  6. //req.Headers.Add
  7.  
  8. HttpWebResponse objResponse = (HttpWebResponse)req.GetResponse();
  9. using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
  10. {
  11. string htmlStuff = sr.ReadToEnd();
  12. sr.Close();
  13. }
  14. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: fulcrum9 is an unknown quantity at this point 
Solved Threads: 0
fulcrum9 fulcrum9 is offline Offline
Newbie Poster

Re: Using StreamReader with sockets for the 2nd time

 
0
  #5
Jul 31st, 2009
@adatapost: If you only want to send&read data once, the code is fine. Otherwise not. That's the problem here.

@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 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.
Last edited by fulcrum9; Jul 31st, 2009 at 12:56 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 380 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC