public ClassCommandInfo(int uidx, ClassUsers userItem) { ThisUser = userItem; userIdx = uidx; ThisUser.Reset(); }
/*============================================================================================ * This is the heart of the server and it kicks * off client threads as they connect * *============================================================================================*/ public void ChatServerThread() { serverRunning = true; IPAddress localAddr = IPAddress.Parse(localIP); serverSocket = new TcpListener(localAddr, localPort); TcpClient clientSocket = default; long counter = 0; serverSocket.Start(); mw.Display("ChatServer Started ....", 5); while (keepRunning) { counter += 1; string dataFromClient = null; byte[] bytesFrom = null; try { // wait for a connection clientSocket = serverSocket.AcceptTcpClient(); bytesFrom = new byte[1024]; NetworkStream networkStream = clientSocket.GetStream(); networkStream.Read(bytesFrom, 0, bytesFrom.Length); } catch (Exception ex) { if (keepRunning) { mw.Display("Socket read error: " + ex.Message, 1); } else { mw.Display("Closing ServerSocket", 5); } } if (bytesFrom != null) { // convert to a string and clean it up dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); dataFromClient = dataFromClient.Replace("\0", string.Empty).Trim(); } string msg = string.Empty; string welcomeMsg; string guestName = "GUEST" + counter.ToString(); if (dataFromClient != null) { if (RequireHello == false && (dataFromClient.Length < 8 || dataFromClient.Substring(0, 7).Equals("[HELLO:") == false)) { // If HELLO is not required msg = dataFromClient; // we're going pass what came in to the client thread // create a guest HELLO string dataFromClient = string.Format("[HELLO:{0}|{0}|_UNK_|_UNK_]", guestName); } if (clientSocket != null) { mw.Display(dataFromClient, 8); if (dataFromClient.Length > 7 && dataFromClient.Substring(0, 7).Equals("[HELLO:")) { // break out the HELLO message dataFromClient = dataFromClient.Replace("[HELLO:", "").Replace("]", "").Trim(); string[] data = dataFromClient.Split('|'); string guid = counter.ToString("D9"); if (data.Length > 3) { // Remote IP address string ipaddr = ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString(); // Set up a guest account - GUID Name Nick IP Computer Name ClassUsers c = new ClassUsers(guid, guestName, guestName, ipaddr, data[3].Trim()); // If client sent a HELLO, try to log in using the transmitted user/password if (msg.Length == 0 && RegistrationCommands.userRegistration.Login(data[0], data[1])) { string g = RegistrationCommands.GetUserInfo("RecID"); if (usersList.FindIndex(x => x.RegisteredGUID.Equals(g)) < 0) { c.FullName = RegistrationCommands.GetUserInfo("FullName"); c.NickName = RegistrationCommands.GetUserInfo("NickName"); c.UserName = RegistrationCommands.GetUserInfo("UserName"); c.RegisteredGUID = RegistrationCommands.GetUserInfo("RecID"); c.Email = RegistrationCommands.GetUserInfo("Email"); c.UserRights = RegistrationCommands.GetUserInfo("Rights"); c.Status = RegistrationCommands.GetUserInfo("Status"); c.registeredDT = RegistrationCommands.GetUserInfo("RegisteredDT"); c.lastOnDT = RegistrationCommands.GetUserInfo("LastOnDT"); c.lastMsgDT = DateTime.Now; welcomeMsg = "Welcome " + (c.IsAdmin ? "administrator " : "") + c.FullName + ".\r\nYou were last on " + c.lastOnDT.ToString("MMMM dd, yyyy @ h:mm tt"); // if there is a Welcome.txt file, load it at the end of the welcome message if (File.Exists(LocalPath + "Welcome.txt")) { welcomeMsg += "\r\n\r\n" + File.ReadAllText(LocalPath + "Welcome.txt"); } } else { // already logged in welcomeMsg = "You are already logged into the system. Your nickname is " + c.NickName; } } else { // login failure c.registeredDT = DateTime.MinValue; c.lastOnDT = DateTime.MinValue; c.lastMsgDT = DateTime.Now; welcomeMsg = "Login failure - you are " + guestName + " and have limited access until logged in"; } // Clear out any change tracks so far c.Reset(); // Add the user to the list usersList.Add(c); // set up access to the client socket clientsHash.Add(guid, clientSocket); // display some information to the form mw.Display(string.Format("{0} -Instance: {1} -User: {2} -Nick: {3} -Computer: {4} -IP: {5} -remoteID: {6} -localIP: {7} -remoteIP: {8})", DateTime.Now.ToString("MM/dd HH:mm:ss"), c.ThreadGUID, c.UserName, c.NickName, c.computerName, c.ipAddress, data[0], data[3], ipaddr) + " is in the lobby"); // Create and start a new thread for this client connection clientThreads.Add(new ClientThreading()); clientThreads[clientThreads.Count - 1].StartClientThread(mw, this, clientSocket, clientThreads.Count - 1, msg); clientThreads[clientThreads.Count - 1].nickName = c.NickName; // Welcome them and let everyone know they are here SendToGUID(guid, welcomeMsg); BroadcastMsgToRoom("", ">>> " + c.NickName + " is in " + c.CurrentRoom); } else { // oops, HELLO msg was too short mw.Display(string.Format(">>> Received badly formed hello message {0}\r\n", dataFromClient), 5); } } else { // tell them we need a hello if (clientSocket.Connected) { try { msg = "This server requires a [HELLO:...] message to initiate a connection"; NetworkStream broadcastStream = clientSocket.GetStream(); Byte[] broadcastBytes = Encoding.ASCII.GetBytes(msg); broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length); broadcastStream.Flush(); // Remote IP address string ipaddr = ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString(); mw.Display(string.Format("Notification for HELLO sent to {0}\r\nReceived: {1}\r\nSent: {2}", ipaddr, dataFromClient, msg)); clientSocket.Dispose(); clientSocket = null; } catch (Exception ex) { mw.Display("Hello broadCast error: " + ex.Message, 1); } } } } } } serverRunning = false; mw.Display("DoServer stopped.", 5); }