public static void receiveCallBack(IAsyncResult ar) { TcpListener server = (TcpListener)ar.AsyncState; Int32 id = 0; ClientData client = new ClientData(); clients.Add(new ClientData()); foreach (ClientData c in clients) //we have to find out how many clients there are { id++; //increment this id number } id--; clients[id].id = id; try { //client.socket = server.EndAcceptTcpClient(ar); old way of doing it. keep for now clients[id].socket = server.EndAcceptTcpClient(ar); // do it this way now? } catch (Exception) { handler.Set(); return; } handler.Set(); //client.stream = client.socket.GetStream(); old way. Keep for now clients[id].stream = clients[id].socket.GetStream(); ManualResetEvent clientHandler = new ManualResetEvent(false); //we set up the connection. so we'll use this to call a callback to send messages while (!Closing.shutDown) //while the server is not trying to close down { clients[id].stream.Read(clients[id].buffer, 0, clients[id].buffer.Length); ProtocolMessage message = decodeMessage(clients[id].buffer); byte[] confirmMessage = System.Text.Encoding.ASCII.GetBytes("command received!"); switch (message.command) { case "ping": //client.stream.WriteAsync(confirmMessage, 0, confirmMessage.Length); clients[id].stream.WriteAsync(confirmMessage, 0, confirmMessage.Length); returnPing(clients[id]); break; case "chat": clients[id].stream.WriteAsync(confirmMessage, 0, confirmMessage.Length); chatReceived(clients[id]); break; case "quit": clients[id].stream.WriteAsync(confirmMessage, 0, confirmMessage.Length); clients[id].stream.Close(); clients[id].socket.Close(); Closing.shutDown = true; handler.Set(); return; default: confirmMessage = System.Text.Encoding.ASCII.GetBytes("command not recognised"); clients[id].stream.WriteAsync(confirmMessage, 0, confirmMessage.Length); break; } } clients[id].stream.Close(); clients[id].socket.Close(); return; }
static void chatReceived(ClientData Client) { }