小白学习贴: C# TCP服务器

xiaoxiao2021-02-28  83

刚入行3周,boss让我写个TCP服务器+客户端… 只是单纯实现功能,等经验丰富后会回头来优化 用户登录的时候是通过Redis缓存服务器来判断,现在还没有接触到和MySQL交互. 如果有大牛经过,劳烦你指出不足

//主函数入口 IRedisClient Redis = RedisManager.GetClient(); listerSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); listerSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6523)); listerSocket.Listen(4); Console.WriteLine("服务器已启动"); OnLoginQuestEvent += new LoginDel(OnLogin); while (true) { Console.WriteLine("服务器正在等待连接"); Socket socketConn = listerSocket.Accept(); socketDic.Add(socketConn.RemoteEndPoint.ToString(), socketConn); Console.WriteLine("服务器连接成功"); Thread thr = new Thread(RecMsg); thr.IsBackground = true; thr.Start(socketConn); try { thrDic.Add(socketConn.RemoteEndPoint.ToString(), thr); } catch (System.Exception e) { Console.WriteLine(e.ToString()); } } /// <summary> /// 处理数据 /// </summary> /// <param name="socket"></param> static void RecMsg(object socket) { Socket clientSocket = (Socket)socket; bool isQuest = true;//每人一个 while (true) { byte[] arrMsgRec = new byte[1024 * 1024]; int len = 0; try { Console.WriteLine("正在等待" + clientSocket.RemoteEndPoint.ToString() + "用户的输入"); len = clientSocket.Receive(arrMsgRec, len, arrMsgRec.Length - len, SocketFlags.None); string count = Encoding.UTF8.GetString(arrMsgRec, 0, len); Console.WriteLine(clientSocket.RemoteEndPoint.ToString() + ":\t" + count); if (count != "ok") { Console.WriteLine(isQuest); if (isQuest) { if (OnLoginQuestEvent != null) { OnLoginQuestEvent(clientSocket, count, ref isQuest); } } } else { isQuest = false; } } catch (SocketException se) { Console.WriteLine(se.Message); socketDic.Remove(clientSocket.RemoteEndPoint.ToString()); thrDic.Remove(clientSocket.RemoteEndPoint.ToString()); break; } } } /// <summary> /// 效验密码 /// </summary> /// <param name="socket"></param> /// <param name="str"></param> public static void OnLogin(Socket socket, string str, ref bool isQuest)//改变用户的isQuest { JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("Puser.txt")); foreach (JsonData temp in jsonData) { JsonData idValue = temp["id"]; JsonData nameValue = temp["name"]; JsonData passworld = temp["passworld"]; JsonData WhetherCon = temp["WhetherCon"]; int id = int.Parse(idValue.ToString()); string uname = nameValue.ToString(); string pwd = passworld.ToString(); string wcon = WhetherCon.ToString(); using (var redisClient = RedisManager.GetClient()) { var user = redisClient.As<User>(); var newUser = new User { Id = Convert.ToInt32(user.GetNextSequence()), Name = uname, passworld = pwd, WhetherCon = wcon }; user.Store(newUser); } if (socket != null && str != null) { JObject jo = (JObject)JsonConvert.DeserializeObject(str); string name = jo["Name"].ToString(); string pwas = jo["passworld"].ToString(); string WerCon = jo["WhetherCon"].ToString(); using (var redisClient = RedisManager.GetClient()) { var user = redisClient.As<User>(); var userList = user.GetAll().Where(b => b.Name.Contains(name)).ToList(); if (userList.Count > 0) { for (int i = 0; i < userList.Count; i++) { if (userList[i].passworld == pwas && userList[i].WhetherCon != "1") { userList[i].WhetherCon = "1"; User ss = new User(); userList[i].WhetherCon = "1"; Console.WriteLine("登录成功"); socket.Send(Encoding.UTF8.GetBytes("CanConnection")); isQuest = false; break; } } if (isQuest == true) { Console.WriteLine("登录失败"); socket.Send(Encoding.UTF8.GetBytes("notConnection")); } } } } } }
转载请注明原文地址: https://www.6miu.com/read-75554.html

最新回复(0)