/// <summary> /// 更新用户列表 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> /// <param name="port"></param> private void UpdateUserList(ClassMsg msg, IPAddress ip, int port) { ClassUsers Users = new ClassUsers(); ClassOptionData OptionData = new ClassOptionData(); DataTable DataReader = OptionData.ExSQLReDr("select * from CurreneyUser"); int i = 0; while (DataReader.Rows.Count != i) { DataRow dtr = DataReader.NewRow(); dtr = DataReader.Rows[i]; ClassUserInfo UserItem = new ClassUserInfo(); //创建并引用ClassUserInfo类 UserItem.UserID = dtr["ID"].ToString(); //记录用户用编号 UserItem.UserIP = dtr["IP"].ToString(); //记录用户的IP地址 UserItem.UserPort = dtr["Port"].ToString(); //记录端口号 UserItem.UserName = dtr["Name"].ToString(); //记录用户名称 UserItem.State = dtr["Sign"].ToString(); //记录当前状态 Users.add(UserItem); //将单用户信息添加到用户列表中 i++; } BinaryFormatter serializer = new BinaryFormatter(); MemoryStream memStream = new MemoryStream(); //创建一个内存流存储区 serializer.Serialize(memStream, Users); //将对象序列化为二进制流 msg.Data = memStream.ToArray();//new ClassSerializers().SerializeBinary(Users).ToArray();//将用户列表写入到二进制流中 //查找当前已上线的用户 DataReader = OptionData.ExSQLReDr("Select * From CurreneyUser Where Sign = " + MsgCommand.Logined); i = 0; while (DataReader.Rows.Count == i)//向所有上线用户发送用户列表 { udpSocket1.Send(IPAddress.Parse(DataReader.Rows[i]["IP"].ToString()), Convert.ToInt16(DataReader.Rows[i]["Port"]), new ClassSerializers().SerializeBinary(msg).ToArray()); i++; } OptionData.Dispose(); }
/// <summary> /// 发送用户信息 /// </summary> /// <param name="msg"></param> /// <param name="ip"></param> /// <param name="port"></param> private void SendUserList(ClassMsg msg, IPAddress ip, int port) { ClassUsers Users = new ClassUsers(); ClassOptionData OptionData = new ClassOptionData(); DataTable DataReader = OptionData.ExSQLReDr("Select * From CurreneyUser"); int i = 0; while (DataReader.Rows.Count != i) { DataRow dtr = DataReader.NewRow(); dtr = DataReader.Rows[i]; ClassUserInfo UserItem = new ClassUserInfo(); //创建并引用ClassUserInfo类 UserItem.UserID = dtr["ID"].ToString(); //记录用户用编号 UserItem.UserIP = dtr["IP"].ToString(); //记录用户的IP地址 UserItem.UserPort = dtr["Port"].ToString(); //记录端口号 UserItem.UserName = dtr["Name"].ToString(); //记录用户名称 UserItem.State = dtr["Sign"].ToString(); //记录当前状态 Users.add(UserItem); //将单用户信息添加到用户列表中 i++; } OptionData.Dispose(); msg.Data = new ClassSerializers().SerializeBinary(Users).ToArray(); udpSocket1.Send(ip, port, new ClassSerializers().SerializeBinary(msg).ToArray()); }