public static List <TcpClient> clients = new List <TcpClient>();//hold clients in list (create group of clients). /// <summary> /// Initialize server & create thread to listen from server /// </summary> static void Main(string[] args) { Console.WriteLine("Server Started"); IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName()); //get host Console.WriteLine("IP : " + GetAddress(Host).ToString()); //get server address TcpListener server = new TcpListener(GetAddress(Host), 5647); //create socket to listen at server at given port number TcpClient ClientSocket; //to hold client socket int ClientNumber = 0; server.Start();//start listening for client's request while (true) { ClientSocket = server.AcceptTcpClient(); //accept client if (clients.Count != 3) //check if the group is full { clients.Add(ClientSocket); //add the client to the group ClientNumber = clients.Count; //assign Client number to Number of clients in clients list Console.WriteLine("Client #" + ClientNumber.ToString() + " started"); HandleClients handle = new HandleClients(ClientSocket, ClientNumber); //handle client } else { byte[] sendError = new byte[1] { 0x00 }; ClientSocket.GetStream().Write(sendError, 0, sendError.Length);//send two bytes to clients to indecate that room is full ClientSocket = null; } } }
static void Main(string[] args) { IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName()); Console.WriteLine(GetAddress(Host).ToString()); TcpListener server = new TcpListener(GetAddress(Host), 5647); TcpClient ClientSocket = default(TcpClient); int ClientsCount = 0; server.Start(); Console.WriteLine("Server Started"); while (true) { ClientsCount++; ClientSocket = server.AcceptTcpClient(); Console.WriteLine("Client #" + ClientsCount.ToString() + " started"); HandleClients handle = new HandleClients(ClientSocket, ClientsCount); } }