| | |
Using StreamReader with sockets for the 2nd time
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
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!
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)
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; } }
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.
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/
Indian Developer
http://falaque.wordpress.com/
I didn't find any problem in your code:
C# Syntax (Toggle Plain Text)
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. c# Syntax (Toggle Plain Text)
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(); } }
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
@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.

@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.
![]() |
Similar Threads
- bind() failed ! Address already in use ?? (C++)
- WeBrowser1 - blank after 2nd click (VB.NET)
- Cant get a software trial to work 2nd time (Windows NT / 2000 / XP)
- 2nd time is a charm! (Windows NT / 2000 / XP)
- Fbkp.exe is trying to access the internet.. (Viruses, Spyware and other Nasties)
- 100% CPU Usage - No Virus, No gaming (Windows NT / 2000 / XP)
- "WINDOWS\ SYSTEM\ BRIDGE.DLL" (Viruses, Spyware and other Nasties)
- My HJT log (Viruses, Spyware and other Nasties)
- WinXP and IE problems. Please Read. (Web Browsers)
Other Threads in the C# Forum
- Previous Thread: Should I struct or should I class
- Next Thread: Fill GridView + C#
Views: 380 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap broadcast button buttons c# check checkbox class client code color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing event excel file files form format forms ftp function gdi+ http httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator path picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






