void WatchConnection() { try { while (isWatching) //注意该循环,服务端要持续监听,要不然一个客户端链接过后就无法链接第二个客户端了。 { //开始监听客户端的消息 //负责通信的socket Socket sokMsg = sokServer.Accept(); //加入通信管理类 // MessageBox.Show(sokMsg.RemoteEndPoint.ToString()); clientManage.AddClient(sokMsg); txtOnlineCount.Text = listChat.Items.Count.ToString(); } } catch (SocketException ex1) { AppendMsg("异常:" + ex1.Message + "2" + DateTime.Now.ToString()); } catch (Exception ex) { AppendMsg("异常:" + ex.Source + "@" + ex.Message + "@" + ex.TargetSite + "@" + ex.InnerException + "@" + ex.Data + "3" + DateTime.Now.ToString()); } }
/// <summary> /// 接收客户端发来的信息 /// </summary> /// <param name="obj"></param> void ReceMsg(object obj) { Socket client; byte[] mag = new byte[1024 * 1024 * 5]; int msgLength; while (true) { client = obj as Socket; msgLength = client.Receive(mag); string sqlconn; //内存流 MemoryStream stream = new MemoryStream(mag, 0, msgLength); //二进制与Object 类的转换 BinaryFormatter fomatter = new BinaryFormatter(); //强制转换并且接收她 var protocolqq = fomatter.Deserialize(stream) as Protocol.ProtocolQQ; switch (protocolqq.mode) { //用户登录 case 0: switch (protocolqq.ope) { //验证登录 case 0: var userLogin = protocolqq.data as Protocol.UserLogin; //弄错了应该是ID,但是我也并没有很想改转来,所以就不改了。 string name = userLogin.Name; string pwd = userLogin.Pwd; //数据库操作语句 sqlconn = "select count(*) from tb_User where ID= '" + name + "'" + "and Pwd = '" + pwd + " '"; //新建一个用户登录返回类 ReturnLogin IsLogin = new ReturnLogin(); //如果num==1则数据库中有该用户 int num = DataOperator.ESSql(sqlconn); if (num != 0) { ClientManager.AddClient(name, client); //判断是否重复登录该用户 if (ClientManager.reLogin == true) { IsLogin.ResultLogin = false; client.Send(SendMsg(0, 1, IsLogin)); //关闭该字 string s_1 = "用户:" + client.RemoteEndPoint.ToString() + "下线。" + "\r\n"; ShowMsg(s_1); receThread.Abort(); while (receThread.ThreadState == ThreadState.Aborted) { Thread.Sleep(100); } break; } else { IsLogin.ResultLogin = true; User user = new User(); user.ID = name; //数据库更新用户记住密码,自动登录操作的操作 sqlconn = "update tb_User set Remember = " + userLogin.Remember + ",AutoLogin="******" where ID = '" + name + " '"; DataOperator.ENQSql(sqlconn); //访问数据库,得到好友列表 List <Friend> friends = new List <Friend>(); sqlconn = "select NickName, HeadID,Sign from tb_User where ID='" + name + "'"; //查询用户信息和用户好友信息的数据库查询语句 SqlDataReader dataReader = DataOperator.GetDataReader(sqlconn); //一直读 if (dataReader.Read()) { if (!(dataReader["NickName"] is DBNull)) { user.NickName = dataReader["NickName"].ToString(); } user.HeadId = Convert.ToInt32(dataReader["HeadID"]); user.Sign = dataReader["Sign"].ToString(); } //加载好友的SQL语句 sqlconn = "select FriendID,NickName,HeadID,Flag from tb_User,tb_Friend where tb_Friend.HostID='" + name + "'" + " and tb_User.ID=tb_Friend.FriendID"; dataReader = DataOperator.GetDataReader(sqlconn); while (dataReader.Read()) { Friend friend = new Friend(); friend.HeadID = Convert.ToInt32(dataReader["HeadID"]); //????? friend.ID = Convert.ToString(dataReader["FriendID"]); friend.NickName = dataReader["NickName"].ToString(); friends.Add(friend); } //关闭读取器 dataReader.Close(); //关闭数据库连接 DataOperator.conn.Close(); IsLogin.Friends = friends; IsLogin.GetUser = user; client.Send(SendMsg(0, 0, IsLogin)); } } else { IsLogin.ResultLogin = false; client.Send(SendMsg(0, 0, IsLogin)); } break; //注册用户 case 1: //不太对得起我自己---这个只允许注册100个 Random rand = new Random(); int v; v = rand.Next(0, 101); var reger = protocolqq.data as Protocol.Regr; reger.ID = Convert.ToString(20000 + v); sqlconn = "Select ID from tb_User Where ID = '" + reger.ID.Trim() + "'"; while (true) { if (DataOperator.ESSql(sqlconn).ToString() != reger.ID) { break; } else { v = rand.Next(0, 100); reger.ID = Convert.ToString(20000 + v); sqlconn = "Select ID from tb_User Where ID ='" + reger.ID + "'"; } } sqlconn = string.Format("insert into tb_User(ID,Pwd, NickName) values('{0}', '{1}','{2}')", reger.ID.Trim(), reger.PSW, reger.NickName); int num_ = DataOperator.ENQSql(sqlconn); if (num_ == 1) { client.Send(SendMsg(0, 2, reger)); } else { reger.ID = ""; client.Send(SendMsg(0, 2, reger)); } break; //忘记密码---验证账号是否正确 case 2: //忘记密码和登录时的协议我们采用一样的 var fuser = protocolqq.data as Protocol.UserLogin; string name_ = fuser.Name.Trim(); //数据库操作语句 sqlconn = "select ID from tb_User where ID= '" + name_ + "'"; int num_1 = DataOperator.ESSql(sqlconn); if (num_1 != 0) { client.Send(SendMsg(0, 3, fuser)); } else { fuser.Name = ""; client.Send(SendMsg(0, 3, fuser)); } break; //忘记密码---修改密码 case 3: var ffuer = protocolqq.data as UserLogin; //修改密码的数据库语句 sqlconn = "update tb_User set Pwd= '" + ffuer.Pwd.Trim() + "'" + "where ID= '" + ffuer.Name + "'"; int num_2 = DataOperator.ENQSql(sqlconn); if (num_2 != 0) { client.Send(SendMsg(0, 4, ffuer)); } else { ffuer.Name = ""; client.Send(SendMsg(0, 4, ffuer)); } break; //用户勾选密码 case 4: break; } break; //用户聊天 case 1: switch (protocolqq.ope) { //普通聊天 case 0: var utu = protocolqq.data as Protocol.UTU; var str = utu.Msg as Str; utu.ReceID = utu.ReceID.Trim(); if (ClientManager.CheckingUser(utu.ReceID)) { ClientManager.SendMsg(mag, ClientManager.client); } //此处的MessageTypeId为1,表示聊天消息;MessageState为0,表示消息未读 //把聊天记录插入数据库 sqlconn = string.Format("INSERT INTO tb_Message (FromUserID, ToUserID, Message, MessageTypeID, MessageState,MessageTime) VALUES ('{0}','{1}','{2}',{3},{4},'{5}')", utu.SendID, utu.ReceID, str.Message, 1, 0, utu.Time); int num = DataOperator.ENQSql(sqlconn); if (num != 1) { //发给客户端 MessageBox.Show("用户:" + utu.SendID + "向用户:" + utu.ReceID + "发送消息失败!" + "/r/n"); } break; // 发送文件 case 1: break; //发送图片 case 2: break; //发送抖屏 case 3: break; } break; //用户离线 case 3: //关闭套接字且移除用户 string s_0 = "用户:" + client.RemoteEndPoint.ToString() + "下线。" + "\r\n"; ClientManager.Clear(client); ShowMsg(s_0); receThread.Abort(); while (receThread.ThreadState == ThreadState.Aborted) { Thread.Sleep(100); } break; } } }