Exemplo n.º 1
0
 public void addUcf(Friend f)
 {
     UCFriend ucf = new UCFriend();
     ucf.Frm = this;
     ucf.CurFriend = f;
     ucf.Top = this.pnFriendsList.Controls.Count * ucf.Height;
     ucf.Width = ucf.Width - 30;
     ucf.myDBClick += ucf_myDBClick;
     this.pnFriendsList.Controls.Add(ucf);
 }
Exemplo n.º 2
0
        public void listen()
        {
            UdpClient uc = new UdpClient(9527);
            while (true)
            {
                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
                byte[] bmsg = uc.Receive(ref ipep);
                IPAddress iip = ipep.Address;
                string msg = Encoding.Default.GetString(bmsg);
                string[] datas = msg.Split('|');
                string msgHead = datas[0];
                switch (msgHead)
                {
                    case "LOGIN":
                        if (datas.Length != 4)
                        {
                            continue;
                        }

                        if (getMyIP().ToString()==ipep.Address.ToString())
                        {
                            continue;
                        }

                        Friend friend = new Friend();

                        int curIndex = Convert.ToInt32(datas[2]);

                        if (curIndex < 0 || curIndex >= _frm.ilHeadImages.Images.Count)
                        {
                            curIndex = 0;
                        }
                        friend.HeadImageIndex = curIndex;
                        friend.NickName = datas[1];
                        friend.Shuoshuo = datas[3];
                        friend.IP = ipep.Address;
                        object[] pars = new object[1];
                        pars[0] = friend;
                        _frm.Invoke(new delAddFriend(_frm.addUcf), pars);

                        //回发,告诉对方我也在,ALSOON|昵称|头像|说说
                        UdpClient ucAlsoon = new UdpClient();
                        string alsoonMsg = "ALSOON|" + getMyIP() + "|3|这个人很懒";
                        byte[] bAlsoonMsg = Encoding.Default.GetBytes(alsoonMsg);
                        ucAlsoon.Send(bAlsoonMsg, bAlsoonMsg.Length, new IPEndPoint(ipep.Address,9527));

                        break;

                    case "ALSOON":
                        if (datas.Length != 4)
                        {
                            continue;
                        }

                        if (getMyIP().ToString()==ipep.Address.ToString())
                        {
                            continue;
                        }

                        Friend alsoFriend = new Friend();

                        int alsoCurIndex = Convert.ToInt32(datas[2]);

                        if (alsoCurIndex < 0 || alsoCurIndex >= _frm.ilHeadImages.Images.Count)
                        {
                            alsoCurIndex = 0;
                        }
                        alsoFriend.HeadImageIndex = alsoCurIndex;
                        alsoFriend.NickName = datas[1];
                        alsoFriend.Shuoshuo = datas[3];
                        alsoFriend.IP = ipep.Address;
                        object[] alsoPars = new object[1];
                        alsoPars[0] = alsoFriend;
                        _frm.Invoke(new delAddFriend(_frm.addUcf), alsoPars);
                    break;

                    case "LOGOUT":
                        Panel pnlist = _frm.getPanel();
                        int deleIndex = 0;
                        //根据当前下线人的IP地址,找到pn中对应的用户控件对象,删除。
                        foreach (UCFriend ItUcf in pnlist.Controls)
                        {
                            if (ItUcf.CurFriend.IP.ToString()==ipep.Address.ToString())
                            {
                                pnlist.Controls.Remove(ItUcf);
                                break;
                            }
                            deleIndex++;
                        }

                        //让其下面的每一个用户控件对象依次上移
                        for (int i = deleIndex + 1; i < pnlist.Controls.Count; i++)
                        {
                            pnlist.Controls[i].Top = i * pnlist.Controls[0].Height;
                        }

                        break;

                    default:
                    break;
                }

            }
        }
Exemplo n.º 3
0
 public FrmChat(Friend f)
 {
     InitializeComponent();
     ucf.CurFriend = f;
 }