Link to home
Start Free TrialLog in
Avatar of DaFou
DaFou

asked on

How to read from a networkstream?

Ola,

In the method GetHTTPrequest I want to write to the concole the text in the stream from the request that came in on the tcp listner.
How do I do that?

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;

namespace AtomHTTPresponse
{
      class AtomHTTPresponse
      {
            private byte[] cByteSteamData = null;
            static void Main(string[] args)
            {
                  AtomHTTPresponse lObjAtomHTTPresponse = new AtomHTTPresponse();
                  TcpListener lObjTcpListner = new TcpListener( 9999 );
                  lObjTcpListner.Start();
                  Console.WriteLine( "Listening started" );
                  TcpClient lObjTcpClient = lObjTcpListner.AcceptTcpClient();
                  Console.WriteLine( "Got a client" );
                  NetworkStream lObjNetworkStream = lObjTcpClient.GetStream();
                  lObjNetworkStream.BeginRead( lObjAtomHTTPresponse.cByteSteamData, 0, 1048576, new AsyncCallback( lObjAtomHTTPresponse.GetHTTPrequest ), null );
            }
            private void GetHTTPrequest( IAsyncResult ar )
            {
                  //Console.WriteLine( "Here I want the HTTP request written" );
            }
            private void RespondHTTP()
            {
                  /*
                  string lStrHTTPheader = "";
                  lStrHTTPheader += "HTTP/1.1 404\r\n";
                  lStrHTTPheader += "Server: c# DHTML Chat Server V0.01alpha\r\n";
                  lStrHTTPheader += "Content-Type: text/html\r\n";
                  lStrHTTPheader += "Accept-Ranges: bytes\r\n";
                  lStrHTTPheader += "Content-Length: 1000000000000000\r\n\r\n"; // Igone this HTTP header for now. We will get to that subject in another question
               
                  Byte[] lArrBtHTTPheader = System.Text.Encoding.ASCII.GetBytes( lStrHTTPheader );
                  
                  lObjNetworkStream.Write( lArrBtHTTPheader, 0, lArrBtHTTPheader.Length );
                  Console.WriteLine( "HTTP header send" );
                  string lStrHTTPcontent = "";
                  lStrHTTPheader += "<html><head><title>yoyo</title></head><body>ohh yeah</body></html>";
               
                  Byte[] lArrBtHTTPcontent = System.Text.Encoding.ASCII.GetBytes( lStrHTTPcontent );

                  lObjNetworkStream.Write( lArrBtHTTPcontent, 0, lArrBtHTTPcontent.Length );
                  Console.WriteLine( "HTTP content send" );
                  */
            }
      }
}
Avatar of dukkorg
dukkorg

You could try:

Console.Write(System.Text.Encoding.ASCII.GetString(lObjAtomHTTPresponse.cByteSteamData));
Avatar of DaFou

ASKER

Compile error
The type or namespace name 'lObjAtomHTTPresponse' could not be found (are you missing a using directive or an assembly reference?)

Console.Write(System.Text.Encoding.ASCII.GetString(      !! lObjAtomHTTPresponse !!     .cByteSteamData));
Sorry My bad. Just do:

Console.Write(System.Text.Encoding.ASCII.GetString(cByteSteamData));
Avatar of DaFou

ASKER

This code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;

namespace AtomHTTPresponse
{
      class AtomHTTPresponse
      {
            private byte[] cByteStreamData = new byte[1048576];
            static void Main(string[] args)
            {
                  AtomHTTPresponse lObjAtomHTTPresponse = new AtomHTTPresponse();
                  TcpListener lObjTcpListner = new TcpListener( 9999 );
                  lObjTcpListner.Start();
                  Console.WriteLine( "Listening started" );
                  TcpClient lObjTcpClient = lObjTcpListner.AcceptTcpClient();
                  Console.WriteLine( "Got a client" );
                  NetworkStream lObjNetworkStream = lObjTcpClient.GetStream();
                  lObjNetworkStream.BeginRead( lObjAtomHTTPresponse.cByteStreamData, 0, 1048576, new AsyncCallback( lObjAtomHTTPresponse.GetHTTPrequest ), null );
            }
            private void GetHTTPrequest( IAsyncResult ar )
            {
                  //Console.WriteLine( "Here I want the HTTP request written" );
                  Console.WriteLine(System.Text.Encoding.ASCII.GetString(cByteStreamData));
            }
      }
}

Renders this result:

C:\_data\Projects\AtomHTTPresponse\bin\Debug>AtomHTTPresponse.exe
Listening started
Got a client

C:\_data\Projects\AtomHTTPresponse\bin\Debug>

THe HTTP request is not being displayed when i connect to my localhost on port 9999 with my browser
ASKER CERTIFIED SOLUTION
Avatar of dukkorg
dukkorg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DaFou

ASKER

Can you please help me out with the folow up to this question on how to minimize this code?
It is quite large now using threadpool and other stuff I really dont want to use YET.

Please partisipate in my next question about this:
https://www.experts-exchange.com/questions/21077813/Making-this-code-as-tiny-as-possible.html

Regards
Avatar of DaFou

ASKER

like do all your magic in GetHTTPrequest would be of great help. plain and simple straigt forward.
It will help my learning process of the C# and the networking

Regards
Well you deleted your new post right before I posted. So I guess I'll still be nice and post it here:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace AtomHTTPresponse
{
      internal sealed class SimpleHttp
      {
            private static void Main(string[] args)
            {
                  TcpListener tcpListner = new TcpListener(9999);
                  tcpListner.Start();
                  Console.WriteLine("Listening started.");

                  TcpClient tcp = tcpListner.AcceptTcpClient();

                  Console.WriteLine("Starting new session.");
                  NetworkStream stream = tcp.GetStream();
                  StreamReader reader = new StreamReader(stream);
                  StreamWriter writer = new StreamWriter(stream);

                  Console.WriteLine("Reading request.");
                  char[] chars = new char[256];
                  reader.Read(chars, 0, chars.Length);
                  Console.Write(chars);

                  Console.WriteLine("Building Response.");
                  string content = "<html><head><title>Response</title></head><body>Here's a response.</body></html>";
                  string header = "HTTP/1.1 200 OK\r\n"
                        + "Cache-Control: private\r\n"
                        + "Content-Type: text/html\r\n"
                        + "Server: SimpleHttp/1.0\r\n"
                        + "Content-Length: " + Encoding.UTF8.GetByteCount(content) + "\r\n"
                        + "Date: " + DateTime.Now.ToLongTimeString() + "\r\n\r\n";
                  
                  Console.WriteLine("Sending response.");
                  writer.Write(header);
                  writer.Write(content);
                  writer.Flush();

                  Console.WriteLine("Cleaning up session.");
                  reader.Close();
                  writer.Close();
                  stream.Close();
                  tcp.Close();

                  Console.WriteLine("Press any key to quit.");
                  Console.Read();
            }
      }
}
Avatar of DaFou

ASKER

I apologise.

I already figurted it out:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.IO;

namespace AtomHTTPresponse
{
      class AtomHTTPresponse
      {
            private byte[] cByteStreamData = new byte[1048576];
            static void Main(string[] args)
            {
                  AtomHTTPresponse lObjAtomHTTPresponse = new AtomHTTPresponse();
                  TcpListener lObjTcpListner = new TcpListener( 9999 );
                  lObjTcpListner.Start();
                  Console.WriteLine( "Listening started" );
                  TcpClient lObjTcpClient = lObjTcpListner.AcceptTcpClient();
                  Console.WriteLine( "Got a client" );
                  NetworkStream lObjNetworkStream = lObjTcpClient.GetStream();
                  
                  StreamReader reader = new StreamReader( lObjNetworkStream );

                  while (reader.Peek() >= 0)
                  {
                        char[] c = new char[256];
                        reader.Read(c, 0, c.Length);
                        Console.Write(c);
                  }

            }
      }
}

This is as small as I can get it now.. from this basis Ill work further
Avatar of DaFou

ASKER

Sorry dukkorg,

that we crossed each other.

Ill be sure to keep posting in this thread the questions I open in folow up to my missions. TO enable you to earn maximum points as my missions progresses
If you want a decent book on learning all of this you can pick this one up used from amazon for cheap. 9 used & new from $5.92

http://www.amazon.com/exec/obidos/ASIN/1861007353/qid%3D1091257973/sr%3D11-1/ref%3Dsr%5F11%5F1/104-4299452-5460711
Avatar of DaFou

ASKER

I want you to have the point you deserver. I insist you take them as you earned them.
To activity on my part I screwed up a bit.

Come get your points please:
https://www.experts-exchange.com/questions/21077819/500-points-for-dukkorg.html
Avatar of DaFou

ASKER

If you could help with my next phase in my mission?

https://www.experts-exchange.com/questions/21077836/Storing-stream-writers.html