private void listen() { UdpClient uc = new UdpClient(9527);//udp协议添加端口号 while (true) { IPEndPoint ipep = new IPEndPoint(IPAddress.Any,0);//将网络端点转化为ip地址 和端口号 byte[] bmsg = uc.Receive(ref ipep);//返回有远程主机发出的udp数据报 string msg = Encoding.Default.GetString(bmsg);//将字节转化为文本 string[] s = msg.Split('|');//元素分隔 if(s.Length != 4) { continue; } if(s[0]=="LOGIN") { Friend friend=new Friend(); int curIndex = Convert.ToInt32(s[2]); if (curIndex<0 || curIndex>=this.ilHeadImages.Images.Count) { curIndex = 0; } friend.HeadImageIndex =curIndex; friend.NickName = s[1]; friend.Shuoshuo=s[3]; object[] pars=new object[1]; pars[0]=friend; this.Invoke(new delAddFriend(this.addUcf),pars[0]); } } }
public void addUcf(Friend f) { UCfriend ucf = new UCfriend(); ucf.Fg = this; ucf.CurFriend = f; ucf.Top = this.pnFriendsList.Controls.Count * ucf.Height; this.pnFriendsList.Controls.Add(ucf); }