943,696 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 912
  • C# RSS
Jul 31st, 2009
0

Using StreamReader with sockets for the 2nd time

Expand Post »
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!

c# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fulcrum9 is offline Offline
3 posts
since Jul 2009
Jul 31st, 2009
0

Re: Using StreamReader with sockets for the 2nd time

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.
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Jul 31st, 2009
0

Re: Using StreamReader with sockets for the 2nd time

I didn't find any problem in your code:
C# Syntax (Toggle Plain Text)
  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);
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 31st, 2009
0

Re: Using StreamReader with sockets for the 2nd time

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.

c# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 31st, 2009
0

Re: Using StreamReader with sockets for the 2nd time

@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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fulcrum9 is offline Offline
3 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Should I struct or should I class
Next Thread in C# Forum Timeline: Fill GridView + C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC