public void Process()
        {
            try
            {
                if (networkStream.DataAvailable)
                {
//					Console.WriteLine("Client #{0} processing", m_Num );
                    int len = networkStream.Read(bytes, 0, (int)bytes.Length);
                    if (len > 0)
                    {
                        length += len;
                        //	Console.Write( "Data {0}, ", len );
                        //	HexViewer.View( bytes, 0, len );
                        //	Console.WriteLine("");
                        ProcessDataReceived();
                    }
                }
                //	else
                //		ProcessDataReceived();
            }
            catch (IOException)
            {
                // All the data has arrived; put it in response.
                ProcessDataReceived();
                networkStream.Flush();
            }
            catch (SocketException)
            {
                networkStream.Close();
                ClientSocket.Close();
                ContinueProcess = false;
                Console.WriteLine("Conection is broken!");
            }
        }          // Process()
        public void StartListening()
        {
            // Client Connections Pool
            ClientConnectionPool ConnectionPool = new ClientConnectionPool();

            // Client Task to handle client requests
            ClientTask = new ClientService(ConnectionPool, numOfThread);

            ClientTask.Start();


            TcpListener listener = null;

            listener = new TcpListener(IPAddress.Any, portNum);
            try
            {
                listener.Start();

                int ClientNbr = 0;

                // Start listening for connections.
                Console.WriteLine("Port {0} waiting for a connection...", portNum);
                while (!stop)
                {
                    //		Thread.Sleep(50);
                    //System.Net.Sockets.Socket socket = listener.AcceptSocket();
                    //			TcpClient tcpHandler = listener.AcceptTcpClient();
                    //TcpClientBis t2 = (TcpClientBis)Convert.ChangeType( tcpHandler, typeof(TcpClientBis));
                    TcpClientBis tcpHandler = new TcpClientBis(listener.AcceptSocket());  // socket );

                    if (tcpHandler != null)
                    {
                        if (tcpHandler.ReceiveBufferSize == 0)
                        {
                            Console.WriteLine("Socket error !");
                            tcpHandler.Close();
                        }
                        //				Console.WriteLine("Client#{0} accepted on port {1}", ++ClientNbr, portNum ) ;
                        ++ClientNbr;
                        // An incoming connection needs to be processed.
                        ConnectionPool.Enqueue(new ClientHandler(tcpHandler, ClientNbr - 1, handler));

                        // --TestingCycle ;
                    }
                    else
                    {
                        break;
                    }
                }
                listener.Stop();

                // Stop client requests handling
                ClientTask.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Exemple #3
0
        public void StartListening()
        {
            // Client Connections Pool
            ClientConnectionPool ConnectionPool = new ClientConnectionPool()  ;

            // Client Task to handle client requests
            ClientTask = new ClientService( ConnectionPool, numOfThread ) ;

            ClientTask.Start() ;

            TcpListener listener = null;
            listener = new TcpListener( IPAddress.Any, portNum );
            try
            {
            listener.Start();

            int ClientNbr = 0 ;

            // Start listening for connections.
            Console.WriteLine("Port {0} waiting for a connection...", portNum );
            while ( !stop )
            {
            //		Thread.Sleep(50);
                //System.Net.Sockets.Socket socket = listener.AcceptSocket();
            //			TcpClient tcpHandler = listener.AcceptTcpClient();
                //TcpClientBis t2 = (TcpClientBis)Convert.ChangeType( tcpHandler, typeof(TcpClientBis));
                TcpClientBis tcpHandler = new TcpClientBis( listener.AcceptSocket() );// socket );

                if (  tcpHandler != null)
                {

                    if ( tcpHandler.ReceiveBufferSize == 0 )
                    {
                        Console.WriteLine( "Socket error !" );
                        tcpHandler.Close();
                    }
            //				Console.WriteLine("Client#{0} accepted on port {1}", ++ClientNbr, portNum ) ;
                    ++ClientNbr;
                    // An incoming connection needs to be processed.
                    ConnectionPool.Enqueue( new ClientHandler( tcpHandler, ClientNbr-1, handler  ) ) ;

                    // --TestingCycle ;
                }
                else
                    break;
            }
            listener.Stop();

            // Stop client requests handling
            ClientTask.Stop() ;
            }
            catch (Exception e)
            {
            Console.WriteLine(e.ToString());
            }
        }