View Single Post
  #1 (permalink)  
Old August 18th, 2004, 12:59 AM
Ed Tijgen Ed Tijgen is offline
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default weird c# or browser behavior

I have a problem with a new C# DHTML chat server I am making. First I'll describe the problem and then i'll post the code.

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


Why is this behavior. On Netscape the behaviour is somewhat similar.
Or more important how do I solve it, or how do I work around it?


this is the C# code.

// 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();
          }
     }
}

Regards,

Ed Tijgen
Reply With Quote