//窗口加载时 private void FormCSharpWinDemo_Load(object sender, EventArgs e) { FrmInformation frm = new FrmInformation(Id, Title); frm.Show(); //获取屏幕宽高与调节最大大小 this.MaximumSize = new Size(543, Screen.GetWorkingArea(this).Height); //监听消息(广播和聊天) ClassStartUdpThread startUdpThread = new ClassStartUdpThread(chatShow); Thread tStartUdpThread = new Thread(new ThreadStart(startUdpThread.StartUdpThread)); tStartUdpThread.IsBackground = true; tStartUdpThread.Start(); //第一次登录时发送广播消息,查看在线用户 ClassBoardCast boardCast = new ClassBoardCast(); boardCast.BoardCast(); ////加载好友列表 //chatShow.Items.Clear(); //Random rnd = new Random(); //for (int i = 0; i < 10; i++) //{ // ChatListItem item = new ChatListItem("分组 " + i); // for (int j = 0; j < 10; j++) // { // ChatListSubItem subItem = new ChatListSubItem("NicName", "好友" + j, "我的个性签名,我的个性签名,我的个性签名"); // subItem.HeadImage = Image.FromFile("head/" + rnd.Next(1, 11) + ".png"); // subItem.ID = (i + 1) * (j + 1); // subItem.Status = (ChatListSubItem.UserStatus)(j % 6); // item.SubItems.AddAccordingToStatus(subItem); // } // item.SubItems.Sort(); // chatShow.Items.Add(item); //} }
//窗体关闭时 private void FrmMain_FormClosed(object sender, FormClosedEventArgs e) { ClassBoardCast cUserQuit = new ClassBoardCast(); cUserQuit.UserQuit(); Application.Exit(); }
//在程序运行后保持监听2425端口,负责处理各种类型消息 public void StartUdpThread() { UdpClient udpClient = new UdpClient(2425); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0); while (true) { byte[] buff = udpClient.Receive(ref ipEndPoint); string userInfo = Encoding.Default.GetString(buff); string msgHead = userInfo.Substring(0, 6);//消息前6位为消息类型标识符 string msgBody = userInfo.Substring(6);//第7位开始为消息实体内容 switch(msgHead) { /*用户第一次登录时发送USER消息到广播地址,收到此类消息会将对方 * 加入到自己的在线好友列表中,并回复对方消息,告诉对方自己在线 */ case ":USER:"******"head/4.png"); subItem.IpAddress = sBody[2]; //在集合中查找用户,没有则加,有则更新信息 if (Chat.GetSubItemsByIp(sBody[2]).Length > 0) { Chat.GetSubItemsByIp(sBody[2])[0] = subItem; } else { if (UserLogin.UserItem.NicName == sBody[0]) { MyNameItem.SubItems.Add(subItem); } else { ListItem.SubItems.Add(subItem); } } //回复消息 ClassBoardCast CReply = new ClassBoardCast(); CReply.BCReply(subItem.IpAddress); } catch(Exception ex) { MessageBox.Show(ex.Message); } break; //聊天消息MESG case ":MESG:": try { string[] mBody = msgBody.Split('|'); string msgName = mBody[0]; string msgID = mBody[1]; string msgIP = mBody[2]; string msgDetail = mBody[3]; //创建一条新线程接收消息 ClassReceiveMsg cRecMsg = new ClassReceiveMsg(msgIP, msgName,msgID, msgDetail); Thread tRecMsg = new Thread(new ThreadStart(cRecMsg.StartRecMsg)); tRecMsg.Start(); } catch { } break; //用户退出时发送QUIT开头的消息 case ":QUIT:": try { //在集合中查找用户,没有则加,有则更新信息 if (Chat.GetSubItemsByNicName(msgBody).Length > 0) { Chat.GetSubItemsByIp(msgBody)[0].OwnerListItem.SubItems.Remove(Chat.GetSubItemsByIp(msgBody)[0]); } } catch { } break; /*自己上线时会向广播发送消息,在接到别人以REPY开头的回复消息时, 将对方加入自己的在线好友列表中*/ case ":REPY:": try { string[] sBody = msgBody.Split(':'); //New一个用户 ChatListSubItem subItem = new ChatListSubItem(sBody[0], sBody[1], sBody[3]); subItem.HeadImage = Image.FromFile("head/4.png"); subItem.IpAddress = sBody[2]; //在集合中查找用户,没有则加,有则更新信息 if (Chat.GetSubItemsByIp(sBody[2]).Length > 0) { Chat.GetSubItemsByIp(sBody[2])[0] = subItem; } else { if (UserLogin.UserItem.NicName == sBody[0]) { MyNameItem.SubItems.Add(subItem); } else { ListItem.SubItems.Add(subItem); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } break; //以DATA开头的消息,表示有人发送文件 case ":DATA:": try { string[] mBody = msgBody.Split('|'); string msgName = mBody[0]; string msgID = mBody[1]; string msgIP = mBody[2]; string msgFileName = mBody[3]; string msgFileLen = mBody[4]; string msgDetail = "【发送文件】" + msgFileName; //创建一条新线程接收消息 ClassReceiveMsg cRecMsg = new ClassReceiveMsg(msgIP, msgName, msgID, msgDetail); Thread tRecMsg = new Thread(new ThreadStart(cRecMsg.StartRecMsg)); tRecMsg.Start(); } catch { } break; //接到以ACEP开头的消息,表示对方同意接收文件 case ":ACEP:": try { string[] mFileBody = msgBody.Split('|'); string mFilePath = mFileBody[3]; string mIP = mFileBody[2]; ClassSendFile cSendFile = new ClassSendFile(mFilePath, mIP); Thread tSendFile = new Thread(new ThreadStart(cSendFile.SendFile)); tSendFile.IsBackground = true; tSendFile.Start(); } catch { } break; } } }