标题:QQ小软件求解
只看楼主
c138926927
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-3-22
结帖率:66.67%
已结贴  问题点数:5 回复次数:11 
QQ小软件求解
测试时遇到的问题,知道的大神给个解决方案啊,谢谢
搜索更多相关主题的帖子: 软件 
2013-03-30 08:43
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:3 
看看你代码的顺序吧,异常的意思是,你在调用send方法的时候,UpdClient已经关闭
2013-03-30 13:04
wzz119
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:62
专家分:136
注 册:2013-1-7
得分:3 
对象已经释放了....没走到这步线程就结束了
2013-03-30 16:39
c138926927
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-3-22
得分:0 
回复 3楼 wzz119
是不是代码顺序问题啊,我检查了,顺序没问题,请问应该怎么解决啊
2013-03-30 18:24
c138926927
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-3-22
得分:0 
回复 2楼 yhlvht
顺序没问题,可能是其他地方的原因吗?我还是没找到解决办法
2013-03-30 18:25
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 
代码贴出来看看嘛
2013-03-30 19:19
c138926927
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-3-22
得分:0 
namespace QQClass
{
    public partial class UDPSocket : UserControl
    {
        private IPEndPoint ServerEndPoint = null;
        private UdpClient UDP_Server = new UdpClient();
        private System.Threading.Thread theUdp;
        public delegate void DataArrivalEventHandler(byte[] Data, IPAddress Ip, int Port);
        public event DataArrivalEventHandler DataArrival;
        private string localHost = "127.0.0.1";
        [Browsable(true), Category("Local"), Description("本地IP地址")]
        public string LocalHost
        {
            get { return localHost; }
            set { localHost = value; }
        }
        private int localPort = 11000;
        [Browsable(true), Category("Local"), Description("本地端口号")]
        public int LocalPort
        {
            get { return localPort; }
            set { localPort = value; }
        }
        private bool active = false;
        [Browsable(true), Category("Local"), Description("激活监听")]
        public bool Active
        {
            get { return active; }
            set
            {
                active = value;
                if (active)
                {
                    OpenSocket();
                }
                else
                {
                    CloseSocket();
                }
            }
        }
        public UDPSocket()
        {
            InitializeComponent();
        }
        public UDPSocket(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        protected void Listener()
        {
            ServerEndPoint = new IPEndPoint(IPAddress.Any, localPort);
            if (UDP_Server != null)
                UDP_Server.Close();
            UDP_Server = new UdpClient(localPort);  //创建一个新的端口号
            UDP_Server.Client.ReceiveBufferSize = 1000000000;   //接收缓冲区大小
            UDP_Server.Client.SendBufferSize = 1000000000;  //发送缓冲区大小
            try
            {
                theUdp = new Thread(new ThreadStart(GetUDPData));
                theUdp.Start();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        private void GetUDPData()
        {
            while (active)
            {
                try
                {
                    byte[] Data = UDP_Server.Receive(ref ServerEndPoint);
                    if (DataArrival != null)
                    {
                        DataArrival(Data, ServerEndPoint.Address, ServerEndPoint.Port);
                    }
                    Thread.Sleep(0);
                }
                catch { }
            }
        }

        private void CallBackMethod(IAsyncResult ar)
        {
            DataArrivalEventHandler dn = (DataArrivalEventHandler)ar.AsyncState;
            dn.EndInvoke(ar);
        }


        public void Send( Host, int Port, byte[] Data)
        {
            try
            {
                IPEndPoint server = new IPEndPoint(Host, Port);
                UDP_Server.Send(Data, Data.Length, server);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void OpenSocket()
        {
            Listener();
        }
        private void CloseSocket()
        {
            if (UDP_Server != null)
                UDP_Server.Close();
            if (theUdp != null)
            {
                Thread.Sleep(30);
                theUdp.Abort();
            }
        }
    }
2013-04-13 19:19
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:0 
代码还不够,报错是在你调用Send方法的时候UDP_Server对象已关闭,你至少要把你在哪调用Send方法贴出来吧,例如你界面上确定按钮的Click事件
2013-04-13 20:55
c138926927
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-3-22
得分:0 
这个是MyQQserver里面的代码,里面也有调用UDPnamespace MyQQServer
红色部分是我书上看见的,但是蓝色部分的LV_SysUser.SelectedItems.Cast中没有Cast,请问是不是代码错了啊?
private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LV_SysUser.SelectedItems.Count > 0)//存在被选择的项
            {
                if (LV_SysUser.SelectedItems.Cast<ListViewItem>().Where<ListViewItem>(item = item.SubItems[4].Text == "12").Count() > 0)//选择项中存在“Close”状态的项
                {
                    if (MessageBox.Show("确定要删除吗?", "软件提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                    {
                        foreach (ListViewItem lvi in LV_SysUser.SelectedItems)//循环所有被选中的项
                        {
                            if (lvi.SubItems[4].Text == "12")//若该用户处于Close状态,则可以删除
                            {
                                ClassOptionData OptionData = new ClassOptionData();//实例化数据操作类
                                OptionData.ExSQL("delete tb_CurreneyUser Where ID = " + lvi.SubItems[0].Text);//删除指定的数据记录
                                LV_SysUser.Items.Remove(lvi);//在ListView控件中移除选中项
                                MessageBox.Show("删除成功!");//提示删除成功
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("在选择的项中,没有处于Close状态的项!");
                }
            }
            else
            {
                MessageBox.Show("没有项被选择!");
            }
        }



namespace MyQQServer
{
    public partial class F_Server : Form
    {
        public F_Server()
        {
            InitializeComponent();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

        }
        private void SendMsgToAll(ClassMsg msg)//发送消息给所有用户
        {
            try
            {
                foreach (System.Windows.Forms.ListViewItem item in this.LV_SysUser.Items)
                {
                     ip = (item.SubItems[1].Text);
                    int port = Convert.ToInt32(item.SubItems[2].Text);
                     stream = new ClassSerializers().SerializeBinary(msg);
                    QQClass.UDPSocket udp = new QQClass.UDPSocket();
                    udp.Send(ip, port, stream.ToArray());
                }
            }
            catch { }

        }
        private void SendMsgToOne( ip, int port, ClassMsg msg)//发送消息给一个用户
        {
            try
            {
                stream = new ClassSerializers().SerializeBinary(msg);
                QQClass.UDPSocket udp = new QQClass.UDPSocket();
                udp.Send(ip, port, stream.ToArray());
            }
            catch { }

        }

        private void 开始服务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (((ToolStripMenuItem)sender).Text == "开始服务")
            {
                ((ToolStripMenuItem)sender).Text = "结束服务";
                udpSocket1.Active = true;
            }
            else
            {
                ((ToolStripMenuItem)sender).Text = "开始服务";
                udpSocket1.Active = false;
            }
        }
        private void Server(bool IsServer)//开始或停止服务
        {

        }  
        private void sockUDP1_DataArrival(byte[] Data, Ip, int Port)
        {
            DataArrivaldelegate outdelegate = new DataArrivaldelegate(DataArrival);
            this.BeginInvoke(outdelegate, new object[] { Data, Ip, Port });
        }

        private delegate void DataArrivaldelegate(byte[] Data, Ip, int Port);

        private void DataArrival(byte[] Data, Ip, int Port) //当有数据到达后的处理进程
        {
            try
            {
                ClassMsg msg = new ClassSerializers().DeSerializeBinary((new (Data))) as ClassMsg;

                switch (msg.sendKind)
                {
                    case SendKind.SendCommand://命令
                        {
                            switch (msg.msgCommand)
                            {
                                case MsgCommand.Registering://注册用户
                                    RegisterUser(msg, Ip, Port);
                                    break;
                                case MsgCommand.Logining://登录用户
                                    UserLogin(msg, Ip, Port, 1);
                                    break;
                                case MsgCommand.UserList://用户列表
                                    SendUserList(msg, Ip, Port);
                                    break;
                                case MsgCommand.SendToOne://发送消息给单用户
                                    SendUserMsg(msg, Ip, Port);
                                    break;
                                case MsgCommand.Close://下线
                                    UpdateUserState(msg, Ip, Port);
                                    break;
                            }
                            break;
                        }
                    case SendKind.SendMsg://消息
                        {
                            switch (msg.msgCommand)
                            {
                                case MsgCommand.SendToOne://发送消息给单用户
                                    SendUserMsg(msg, Ip, Port);
                                    break;
                            }
                            break;
                        }
                    case SendKind.SendFiel://文件
                        {
                            break;
                        }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void UserLogin(ClassMsg msg, Ip, int Port, int State)
        {
            RegisterMsg registermsg = (RegisterMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data));

            ClassOptionData OptionData = new ClassOptionData();
            MsgCommand msgState = msg.msgCommand;   
            String UserName = registermsg.UserName;
            String PassWord = registermsg.PassWord;
            String vIP = Ip.ToString();

            SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where Name = " + "'" + UserName + "'" + " and PassWord = "
                            + "'" + PassWord + "'");

            DataReader.Read();
            string ID = Convert.ToString(DataReader.GetInt32(0));
            if (DataReader.HasRows)
            {
                OptionData.ExSQL("Update tb_CurreneyUser Set Sign = " + Convert.ToString((int)(MsgCommand.Logined)) + ",IP = " + "'" + vIP + "',Port = " + "'" + Port.ToString() + "'" + " Where ID = " + ID);
                msg.msgCommand = MsgCommand.Logined;
                msg.SID = ID;
                SendMsgToOne(Ip, Port, msg);
                UpdateUserState(msg, Ip, Port);
            }
            OptionData.Dispose();
            UpdateUser();
        }
        private void RegisterUser(ClassMsg msg, Ip, int Port)
        {
            msg = InsertUser(msg, Ip, Port);
            UpdateUserList(msg, Ip, Port);
        }
        private ClassMsg InsertUser(ClassMsg msg, Ip, int Port)
        {
            RegisterMsg registermsg = (RegisterMsg)new ClassSerializers().DeSerializeBinary(new MemoryStream(msg.Data));

            ClassOptionData OptionData = new ClassOptionData();
            MsgCommand Sate = msg.msgCommand;
            String UserName = registermsg.UserName;
            String PassWord = registermsg.PassWord;
            String vIP = Ip.ToString();
            OptionData.ExSQL("insert into tb_CurreneyUser (IP,Port,Name,PassWord,Sign) values ('" + vIP + "'," +
                Port.ToString() + ",'" + UserName + "','" + PassWord + "'," + Convert.ToString((int)(MsgCommand.Registered)) + ")");
            SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");
            UpdateUser();
            OptionData.Dispose();
            msg.msgCommand = MsgCommand.Registered;
            SendMsgToOne(Ip, Port, msg);
            return msg;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateUser();
        }
        private void UpdateUser()
        {
            ClassOptionData OptionData = new ClassOptionData();
            SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");
            LV_SysUser.Items.Clear();
            while (DataReader.Read())
            {
                ListViewItem listItem = new ListViewItem();
                listItem.Text = Convert.ToString(DataReader.GetInt32(0));
                listItem.SubItems.Add(DataReader.GetString(1));
                listItem.SubItems.Add(Convert.ToString(DataReader.GetInt32(2)));
                listItem.SubItems.Add(DataReader.GetString(3));
                listItem.SubItems.Add(Convert.ToString(DataReader.GetInt32(5)));
                LV_SysUser.Items.Add(listItem);
            }
            OptionData.Dispose();
        }
        private void F_Server_FormClosed(object sender, FormClosedEventArgs e)
        {
            udpSocket1.Active = false;
            ClassOptionData OptionData = new ClassOptionData();
            OptionData.ExSQL("Update tb_CurreneyUser Set Sign =12 Where ID >0");
            OptionData.Dispose();

        }

        private void udpSocket1_DataArrival(byte[] Data, IPAddress Ip, int Port)
        {
            DataArrivaldelegate outdelegate = new DataArrivaldelegate(DataEvent);
            this.BeginInvoke(outdelegate, new object[] { Data, Ip, Port });

        }
        private void DataEvent(byte[] Data, Ip, int Port)
        {
            //MessageBox.Show(Encoding.Unicode.GetString(Data));
            //this.Text = Encoding.Unicode.GetString(Data);
            //MessageBox.Show(this.Text);
        }

        private void SendUserList(ClassMsg msg, Ip, int Port)
        {
            ClassUsers Users = new ClassUsers();
            ClassOptionData OptionData = new ClassOptionData();
            SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");

            while (DataReader.Read())
            {
                ClassUserInfo UserItem = new ClassUserInfo();
                UserItem.UserID = Convert.ToString(DataReader.GetInt32(0));
                UserItem.UserIP = DataReader.GetString(1);
                UserItem.UserPort = Convert.ToString(DataReader.GetInt32(2));
                UserItem.UserName = DataReader.GetString(3);
                UserItem.State = Convert.ToString(DataReader.GetInt32(5));
                Users.add(UserItem);
            }
            OptionData.Dispose();
            msg.Data = new ClassSerializers().SerializeBinary(Users).ToArray();
            udpSocket1.Send(Ip, Port, new ClassSerializers().SerializeBinary(msg).ToArray());
        }

        private void UpdateUserList(ClassMsg msg, Ip, int Port)
        {
            ClassUsers Users = new ClassUsers();
            ClassOptionData OptionData = new ClassOptionData();
            SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser");

            while (DataReader.Read())
            {
                ClassUserInfo UserItem = new ClassUserInfo();
                UserItem.UserID = Convert.ToString(DataReader.GetInt32(0));
                UserItem.UserIP = DataReader.GetString(1);
                UserItem.UserPort = Convert.ToString(DataReader.GetInt32(2));
                UserItem.UserName = DataReader.GetString(3);
                UserItem.State = Convert.ToString(DataReader.GetInt32(5));
            }

            msg.Data = new ClassSerializers().SerializeBinary(Users).ToArray();
            DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where Sign = " + MsgCommand.Logined);
            while (DataReader.Read())
            {

                udpSocket1.Send(IPAddress.Parse(DataReader.GetString(1)), DataReader.GetInt32(2), new ClassSerializers().SerializeBinary(msg).ToArray());
            }
            OptionData.Dispose();
        }

        private void SendUserMsg(ClassMsg msg, Ip, int Port)

        {
            ClassOptionData OptionData = new ClassOptionData();
            SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where ID = " + msg.RID);
            DataReader.Read();
            string ip = DataReader.GetString(1);
            int port = DataReader.GetInt32(2);
            udpSocket1.Send(IPAddress.Parse(ip), port, new ClassSerializers().SerializeBinary(msg).ToArray());
            OptionData.Dispose();
            DataReader.Dispose();
        }
        private void UpdateUserState(ClassMsg msg, Ip, int Port)
        {

            ClassOptionData OptionData = new ClassOptionData();
            OptionData.ExSQL("Update tb_CurreneyUser Set Sign = " + Convert.ToString((int)(msg.msgCommand)) + " Where ID = " + msg.SID);
            SqlDataReader DataReader = OptionData.ExSQLReDr("Select * From tb_CurreneyUser Where Sign = " + Convert.ToString((int)(MsgCommand.Logined)));
            if (msg.msgCommand == MsgCommand.Close)
                msg.msgID = "Down";
            else if (msg.msgCommand == MsgCommand.Logined)
                msg.msgID = "Up";
            msg.msgCommand = MsgCommand.UpdateState;
            while (DataReader.Read())
            {
                udpSocket1.Send(IPAddress.Parse(DataReader.GetString(1)),
                        DataReader.GetInt32(2), new ClassSerializers().SerializeBinary(msg).ToArray());
            }
            OptionData.Dispose();
            UpdateUser();
        }



        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
2013-04-14 12:01
c138926927
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-3-22
得分:0 
这个是Click代码部分
        private void button_OK_Click(object sender, EventArgs e)
        {
            if (text_PassWord.Text.Trim() == text_PassWord2.Text.Trim())
            {
                RegisterMsg registermsg = new RegisterMsg();
                registermsg.UserName = text_Name.Text;
                registermsg.PassWord = text_PassWord.Text;
                byte[] registerData = new ClassSerializers().SerializeBinary(registermsg).ToArray();
                ClassMsg msg = new ClassMsg();
                msg.sendKind = SendKind.SendCommand;
                msg.msgCommand = MsgCommand.Registering;
                msg.Data = registerData;
                serID = text_IP.Text.Trim();
                udpSocket1.Send(IPAddress.Parse(serID), Convert.ToInt32(text_IP5.Text.Trim()), new ClassSerializers().SerializeBinary(msg).ToArray());
            }
            else
            {
                text_PassWord.Text = "";
                text_PassWord.Text = "";
                MessageBox.Show("密码与确认密码不匹配,请重新输入");
            }
        }
2013-04-14 12:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-402336-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.146877 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved