public void receiveMsg() { UdpClient uc = new UdpClient(9527); while (true) { IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 0); byte[] btMsg = uc.Receive(ref ipe); string receiveMsg = Encoding.Default.GetString(btMsg); string[] datas = receiveMsg.Split('|'); if (datas[0] == "LOGIN") { if (datas.Length != 4) { continue; } friend frd = new friend(); frd.NickName = datas[1]; frd.HeadImage = Convert.ToInt32(datas[2]); frd.ShuoShuo = datas[3]; frd.FriendIP = ipe.Address; frd.IsOpen = false; deleAddFriend deleadd = new deleAddFriend(AddFriend); _frm.Invoke(deleadd, frd); UdpClient uca = new UdpClient(); string astr = "ALSOON|" + _frm.txtNick.Text + "|11|" + _frm.labShuoShuo.Text; byte[] abt = Encoding.Default.GetBytes(astr); IPEndPoint aipe = new IPEndPoint(ipe.Address,9527); uca.Send(abt,abt.Length,aipe); //sendMsgToAll("ALSOON"); } if (datas[0] == "MSG") { foreach (friend frd in _frm.friendList) { if (frd.FriendIP.ToString()==ipe.Address.ToString()) { if (frd.IsOpen==false) { frd.msgList.Add(datas[2]+":"+datas[1]+"\r\n"); } if (frd.IsOpen == true) { frd.Fc.txtTalkMain.AppendText(datas[2]+":"+datas[1]+"\r\n"); } } } //收到谁的消息,谁的头像闪动 foreach (UCFrdList ucf in _frm.pnFrdList.Controls) { if (ucf.CurFriend.FriendIP.ToString() == ipe.Address.ToString()&&ucf.CurFriend.IsOpen==false) { //ucf.StartFlash(); delegateStart mydele = new delegateStart(Start); ucf.Invoke(mydele,ucf); } } } if (datas[0] == "ALSOON") { if (datas.Length != 4) { continue; } IPAddress localIP = GetLocalIP(); if (ipe.Address.ToString() == localIP.ToString()) { continue; } friend alfrd = new friend(); alfrd.NickName = datas[1]; alfrd.HeadImage = Convert.ToInt32(datas[2]); alfrd.ShuoShuo = datas[3]; alfrd.FriendIP = ipe.Address; alfrd.IsOpen = false; deleAddFriend deleadd = new deleAddFriend(AddFriend); _frm.Invoke(deleadd, alfrd); } } }
public void AddFriend(friend frd) { UCFrdList ucf = new UCFrdList(); ucf.Frm = _frm; ucf.CurFriend = frd; ucf.Top=_frm.pnFrdList.Controls.Count * ucf.Height; ucf.myDBClick += new EventHandler(ucf_myDBClick); _frm.pnFrdList.Controls.Add(ucf); _frm.friendList.Add(ucf.CurFriend); }