public void Receive() { try { byte[] buffer = new byte[BUFFER_SIZE]; int n = conversationSocket.Receive(buffer); /* * 检验收到的是不是登录或注册数据包,如果不是,直接退出 * 解析登录或注册数据包 * 修改自身的ID等信息 * 如果是注册数据包,那么则调用DbLinker添加进数据库并取得返回值(是否成功) * 如果失败直接退出 */ LoginDataPackage loginData = DataPackage.Parse(buffer) as LoginDataPackage; if (loginData == null) { throw new Exception("非法数据包"); } if (DbLinker.HasUser(loginData.UserID)) //有则登录 { if (loginData.Password != DbLinker.GetUPwd(loginData.UserID)) { throw new Exception("密码错误"); } } else //没有则添加好友 { DbLinker.AddUser(loginData.UserID, loginData.Password, 0); } while (true) { buffer = new byte[BUFFER_SIZE]; n = conversationSocket.Receive(buffer); string words = Encoding.UTF8.GetString(buffer, 0, n); Program.ShowMsg(words); onMessageArrive?.Invoke(words); //C#事件触发订阅器执行处理 } }catch (Exception ex) { Program.ShowMsg("Error: " + ex.Message); Send("Error: " + ex.Message); Program.onlineUsers.Remove(this); } }
static bool showMessage = true; //Console will show the dialog if it's true. static void Main(string[] args) { DbLinker.Init(); InitListner(); Console.ReadLine(); }