Hi all,

I am trying to make a very efficient C# DHTML chat server. But even the
responding to clients bogles me. I will post the code after I have described
the problem:

When i open 3 new instances of IE and connect the first client to
localhost:9999 all the status bar displays is:
"opening page......."
It does not display "1" as expected. But when the 2nd client connects the
1st client displays "2" and the 2nd client displays "opening page"
Only when the 3rd client connects the 2nd client displays "2" the first
client displays "3" and the 3rd client displays "1" ( because the programm
now terminated. )
But when I restart the client and reuse the same 3 clients ( without closing
them and opening new instances of IE ) the first client after connecting
displays "1" imediately ( as should be ).
when connecting the 2nd client it imediatly displays "1" and the 1st client
dispays "2" and so on and so on.

Is IE or C# so fucked up or am I doing something wrong?


// console application
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.IO;
using System.Text;

namespace AtomHTTPresponse
{
class AtomHTTPresponse
{
private const int MAX_CONNECTIONS = 3;
private ArrayList prArrayListHTTPpipes = new ArrayList();
private TcpListener prTcpListener = new TcpListener( IPAddress.Parse(
"127.0.0.1" ), 9999 );
static void Main(string[] args)
{
AtomHTTPresponse lAtomHTTPresponse = new AtomHTTPresponse();
Thread lThreadListener = new Thread( new ThreadStart(
lAtomHTTPresponse.ListenerHandler ) );
lThreadListener.Start();
}
private void ListenerHandler()
{
prTcpListener.Start();
for ( int lCntOuter = 0; lCntOuter < MAX_CONNECTIONS; lCntOuter++ )
{
//lock ( this )
//{
Thread lThreadListener = new Thread( new ThreadStart( ConnectionHandler
) );
lThreadListener.Start();
//}
}
}
private void ConnectionHandler()
{
TcpClient lTcpClient = prTcpListener.AcceptTcpClient();
NetworkStream lNetworkStream = lTcpClient.GetStream();
StreamReader lStreamReader = new StreamReader( lNetworkStream );
StreamWriter lStreamWriter = new StreamWriter( lNetworkStream );

HTTPpipe lHTTPpipe = new HTTPpipe( lTcpClient, lNetworkStream,
lStreamReader, lStreamWriter );
prArrayListHTTPpipes.Add( lHTTPpipe );

lHTTPpipe.WriteToCLient( "<script language=\"javascript\">window.status =
0</script>" );

for ( int lCntInner = 0; lCntInner < prArrayListHTTPpipes.Count;
lCntInner++ )
{
( prArrayListHTTPpipes[ lCntInner ] as HTTPpipe ).WriteToCLient(
"<script language=\"javascript\">window.status = parseInt( window.status ) +
1</script>" );
}
}
}
class HTTPpipe
{
public TcpClient puTcpClient;
public NetworkStream puNetworkStream;
public StreamReader puStreamReader;
public StreamWriter puStreamWriter;
public char[] puArrCharRequest = new char[256];

static string cStrResponse = "<script
language=\"javascript\">window.status = 0</script>";

static string cStrHTTPheader = "HTTP/1.1 200 OK\r\n"
+ "Server: Microsoft-IIS/5.0\r\n"
+ "Date: Wed, 9 jan 2004 19:59:09 GMT\r\n"
+ "X-Powered-By: ASP.NET\r\n"
+ "Content-Type: text/html\r\n"
+ "Set-Cookie: ASPSESSIONIDASQSSARR=CCOBBLCBOLKOECFIANBGOBAI; path=/\r\n"
// let it be for now. redundancy will be removed at a later time
+ "Cache-control: private\r\n\r\n";

public HTTPpipe( TcpClient iTcpClient, NetworkStream iNetworkStream,
StreamReader iStreamReader, StreamWriter iStreamWriter )
{
puTcpClient = iTcpClient;
puNetworkStream = iNetworkStream;
puStreamReader = iStreamReader;
puStreamWriter = iStreamWriter;
puStreamReader.Read( puArrCharRequest, 0, puArrCharRequest.Length ); //
do nothing with the request but now we know we dont send a response before
the request has been fully received.
WriteToCLient( cStrHTTPheader );
WriteToCLient( cStrResponse );
}
public void WriteToCLient( string iStrResponse )
{
puStreamWriter.Write( iStrResponse );
puStreamWriter.Flush();
}
public void Close()
{
puTcpClient.Close();
}
}
}