private void Process() { while (ContinueProcess) { ClientHandler client = null; lock (ConnectionPool.SyncRoot) { if (ConnectionPool.Count > 0) { client = ConnectionPool.Dequeue(); } } if (client != null) { client.Process(); // Provoke client // if client still connect, schedulor later processingle it if (client.Alive) { ConnectionPool.Enqueue(client); } /*else * Console.WriteLine("dequeue client#{0}",client.m_Num );*/ } Thread.Sleep(50); } //Console.WriteLine( "Client service ended" ); }
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()); } }
public void StartListening() { ClientService ClientTask; // Client Connections Pool ClientConnectionPool ConnectionPool = new ClientConnectionPool(); // Client Task to handle client requests ClientTask = new ClientService(ConnectionPool); ClientTask.Start(); string DataToSend = ""; new Thread(() => { while (DataToSend != "quit") { Console.WriteLine("\nType a text to be sent:"); DataToSend = Console.ReadLine(); if (DataToSend.Length != 0) { ClientTask.BroadcastMsg(DataToSend); } } }).Start(); TcpListener listener = new TcpListener(IPAddress.Parse(adress), portNum); try { listener.Start(); int ClientNbr = 0; // Start listening for connections. Console.WriteLine("Waiting for a connection..."); while (true) { TcpClient handler = listener.AcceptTcpClient(); if (handler != null) { Console.WriteLine("Client#{0} accepted!", ++ClientNbr); // An incoming connection needs to be processed. ConnectionPool.Enqueue(new ClientHandler(handler, ClientTask)); } else { break; } } listener.Stop(); ClientTask.Stop(); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); }
private void Process() { while (ContinueProcess) { ClientHandler client = null; lock (ConnectionPool.SyncRoot) { if (ConnectionPool.Count > 0) { client = ConnectionPool.Dequeue(); } } if (client != null) { client.Process(); if (client.StatusClient == Status.Waiting && !waitingPool.Contains(client)) { waitingPool.Add(client); } if (client.StatusClient != Status.Guest && !verifiedPool.Contains(client)) { verifiedPool.Add(client); } if (client.Alive) { lock (onlinePool) { if (!onlinePool.Contains(client)) { onlinePool.Add(client); } } ConnectionPool.Enqueue(client); } else { ClientDisconnect(client); Console.WriteLine(client.Name + " disconnected"); } } Thread.Sleep(100); } }
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()); } }