private static void ProcessUserRead(IAsyncResult result) { TcpUserCommunication currentConnection = (TcpUserCommunication)result.AsyncState; User userObj = currentConnection.User; //NetworkStream stream = (NetworkStream)result; int bytesRead = currentConnection.Stream.EndRead(result); UTF8Encoding encoder = new UTF8Encoding(); string userInput = encoder.GetString(currentConnection.readingBytes, 0, bytesRead).Trim(); HandleClientCommunication(userObj, userInput); AsyncCallback callBack = new AsyncCallback(ProcessUserRead); currentConnection.Stream.BeginRead(currentConnection.readingBytes, 0, 4096, callBack, currentConnection); }
private static void ListenForClients() { tcpListener.Start(); while (true) { //should talker move to async? TcpClient client = tcpListener.AcceptTcpClient(); Random userNamer = new Random(); int userNumber = userNamer.Next(0, 300); TcpUserCommunication tcpClient = new TcpUserCommunication(client); User UserObj = new User(new List<IUserConnection>() { tcpClient }, userNumber); tcpClient.User = UserObj; AsyncCallback callBack = new AsyncCallback(ProcessUserRead); client.GetStream().BeginRead(tcpClient.readingBytes, 0, 4096, callBack, tcpClient); ConnectUser(UserObj, tcpClient); } }