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.AcceptTcpCl ient();
Console.WriteLine( "Got a client" );
NetworkStream lObjNetworkStream = lObjTcpClient.GetStream();
lObjNetworkStream.BeginRea d( lObjAtomHTTPresponse.cByte SteamData, 0, 1048576, new AsyncCallback( lObjAtomHTTPresponse.GetHT TPrequest ), 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></he ad><body>o hh yeah</body></html>";
Byte[] lArrBtHTTPcontent = System.Text.Encoding.ASCII .GetBytes( lStrHTTPcontent );
lObjNetworkStream.Write( lArrBtHTTPcontent, 0, lArrBtHTTPcontent.Length );
Console.WriteLine( "HTTP content send" );
*/
}
}
}
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.AcceptTcpCl
Console.WriteLine( "Got a client" );
NetworkStream lObjNetworkStream = lObjTcpClient.GetStream();
lObjNetworkStream.BeginRea
}
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";
Byte[] lArrBtHTTPheader = System.Text.Encoding.ASCII
lObjNetworkStream.Write( lArrBtHTTPheader, 0, lArrBtHTTPheader.Length );
Console.WriteLine( "HTTP header send" );
string lStrHTTPcontent = "";
lStrHTTPheader += "<html><head><title>yoyo</
Byte[] lArrBtHTTPcontent = System.Text.Encoding.ASCII
lObjNetworkStream.Write( lArrBtHTTPcontent, 0, lArrBtHTTPcontent.Length );
Console.WriteLine( "HTTP content send" );
*/
}
}
}
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.A SCII.GetSt ring( !! lObjAtomHTTPresponse !! .cByteSteamData));
The type or namespace name 'lObjAtomHTTPresponse' could not be found (are you missing a using directive or an assembly reference?)
Console.Write(System.Text.
Sorry My bad. Just do:
Console.Write(System.Text. Encoding.A SCII.GetSt ring(cByte SteamData) );
Console.Write(System.Text.
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.AcceptTcpCl ient();
Console.WriteLine( "Got a client" );
NetworkStream lObjNetworkStream = lObjTcpClient.GetStream();
lObjNetworkStream.BeginRea d( lObjAtomHTTPresponse.cByte StreamData , 0, 1048576, new AsyncCallback( lObjAtomHTTPresponse.GetHT TPrequest ), null );
}
private void GetHTTPrequest( IAsyncResult ar )
{
//Console.WriteLine( "Here I want the HTTP request written" );
Console.WriteLine(System.T ext.Encodi ng.ASCII.G etString(c ByteStream Data));
}
}
}
Renders this result:
C:\_data\Projects\AtomHTTP response\b in\Debug>A tomHTTPres ponse.exe
Listening started
Got a client
C:\_data\Projects\AtomHTTP response\b in\Debug>
THe HTTP request is not being displayed when i connect to my localhost on port 9999 with my browser
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.AcceptTcpCl
Console.WriteLine( "Got a client" );
NetworkStream lObjNetworkStream = lObjTcpClient.GetStream();
lObjNetworkStream.BeginRea
}
private void GetHTTPrequest( IAsyncResult ar )
{
//Console.WriteLine( "Here I want the HTTP request written" );
Console.WriteLine(System.T
}
}
}
Renders this result:
C:\_data\Projects\AtomHTTP
Listening started
Got a client
C:\_data\Projects\AtomHTTP
THe HTTP request is not being displayed when i connect to my localhost on port 9999 with my browser
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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("Listeni ng started.");
TcpClient tcp = tcpListner.AcceptTcpClient ();
Console.WriteLine("Startin g 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("Buildin g Response.");
string content = "<html><head><title>Respon se</title> </head><bo dy>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.ToLongTimeStr ing() + "\r\n\r\n";
Console.WriteLine("Sending response.");
writer.Write(header);
writer.Write(content);
writer.Flush();
Console.WriteLine("Cleanin g up session.");
reader.Close();
writer.Close();
stream.Close();
tcp.Close();
Console.WriteLine("Press any key to quit.");
Console.Read();
}
}
}
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("Listeni
TcpClient tcp = tcpListner.AcceptTcpClient
Console.WriteLine("Startin
NetworkStream stream = tcp.GetStream();
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
Console.WriteLine("Reading
char[] chars = new char[256];
reader.Read(chars, 0, chars.Length);
Console.Write(chars);
Console.WriteLine("Buildin
string content = "<html><head><title>Respon
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
+ "Date: " + DateTime.Now.ToLongTimeStr
Console.WriteLine("Sending
writer.Write(header);
writer.Write(content);
writer.Flush();
Console.WriteLine("Cleanin
reader.Close();
writer.Close();
stream.Close();
tcp.Close();
Console.WriteLine("Press any key to quit.");
Console.Read();
}
}
}
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.AcceptTcpCl ient();
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
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.AcceptTcpCl
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
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
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
http://www.amazon.com/exec/obidos/ASIN/1861007353/qid%3D1091257973/sr%3D11-1/ref%3Dsr%5F11%5F1/104-4299452-5460711
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
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
ASKER
If you could help with my next phase in my mission?
https://www.experts-exchange.com/questions/21077836/Storing-stream-writers.html
https://www.experts-exchange.com/questions/21077836/Storing-stream-writers.html
Console.Write(System.Text.