private void SendMessage() { if (rtfSend.Text == "") { rtfSend.Text = "请输入发送消息..."; rtfSend.BackColor = Color.OldLace; isTextBoxNotEmpty = false; rtfSend.ReadOnly = true; } if (isTextBoxNotEmpty) { try { IPEndPoint ipport = new IPEndPoint(IPAddress.Parse(Remoteip), PreDefine.WAW_DEFAULTPORT); MsgSend sendmsg = new MsgSend(ipport, wawCMD.WAW_SENDMSG, rtfSend.Rtf); sendmsg.Send(); sendmsg.SendClose(); rtfRcv.AppendTextAsRtf(Localuser + " " + DateTime.Now.ToLongTimeString() + "\r\n", new Font(Font, FontStyle.Regular), RtfColor.Green); rtfRcv.AppendTextAsRtf(" "); rtfRcv.AppendRtf(rtfSend.Rtf); rtfRcv.Select(rtfRcv.Text.Length, 0); rtfRcv.ScrollToCaret(); rtfSend.Text = string.Empty; } catch (Exception ex) { rtfRcv.AppendText("发送消息失败!" + "\r\n"); } } }
private void DataConfig(object Sender, MessageEventArgs e) { byte[] ver_bytes = new byte[16]; byte[] serialnum_bytes = new byte[8]; byte[] user_bytes = new byte[50]; byte[] hostname_bytes = new byte[50]; byte[] cmd_bytes = new byte[4]; string user = string.Empty; string hostname = string.Empty; string msgstr = string.Empty; MsgDistribute distri = null; IPEndPoint remote_ipend = e.remoteIP; byte[] rec_buff = e.buffer; Array.Copy(rec_buff, 0, ver_bytes, 0, 16); Array.Copy(rec_buff, 16, serialnum_bytes, 0, 8); Array.Copy(rec_buff, 24, user_bytes, 0, 50); Array.Copy(rec_buff, 74, hostname_bytes, 0, 50); Array.Copy(rec_buff, 124, cmd_bytes, 0, 4); wawCMD cmd = (wawCMD)BitConverter.ToUInt32(cmd_bytes, 0); List <byte> bytesource = rec_buff.ToList(); bytesource.RemoveRange(0, 128); byte[] message = bytesource.ToArray(); user = Encoding.UTF8.GetString(user_bytes); hostname = Encoding.UTF8.GetString(hostname_bytes); msgstr = Encoding.UTF8.GetString(message); //是否为广播消息 if ((cmd == wawCMD.WAW_BC_ABSENCE) || (cmd == wawCMD.WAW_BC_BUSY) || (cmd == wawCMD.WAW_BC_CHECKNEWVER) || (cmd == wawCMD.WAW_BC_GETLIST) || (cmd == wawCMD.WAW_BC_SIGNIN) || (cmd == wawCMD.WAW_BC_SIGNOUT)) { switch (cmd) { case wawCMD.WAW_BC_ABSENCE: break; case wawCMD.WAW_BC_BUSY: break; case wawCMD.WAW_BC_CHECKNEWVER: break; case wawCMD.WAW_BC_GETLIST: try { ListViewItem item = new ListViewItem(); ListViewItem.ListViewSubItem subitem_user = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_ip = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_hostname = new ListViewItem.ListViewSubItem(); subitem_user.Text = Encoding.UTF8.GetString(user_bytes);//此为第二列,listviewitem的名称显示在第一列 subitem_ip.Text = remote_ipend.Address.ToString(); subitem_hostname.Text = Encoding.UTF8.GetString(hostname_bytes); item.SubItems.Add(subitem_user); item.SubItems.Add(subitem_ip); item.SubItems.Add(subitem_hostname); item.Tag = (object)wawState.SignIn; //存储用户当前状态 UserlistUpdate(item, 1); UserCountUpdate(); //回复消息 MsgSend signinreply = new MsgSend(remote_ipend, wawCMD.WAW_SENDSTATE, frmMain.Userstate.ToString()); signinreply.Send(); signinreply.SendClose(); } catch (Exception ex) { MessageBox.Show(ex.Message, "消息接收处理WAW_BC_GETLIST出错"); } break; case wawCMD.WAW_BC_SIGNIN: try { ListViewItem item = new ListViewItem(); ListViewItem.ListViewSubItem subitem_user = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_ip = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_hostname = new ListViewItem.ListViewSubItem(); subitem_user.Text = Encoding.UTF8.GetString(user_bytes);//此为第二列,listviewitem的名称显示在第一列 subitem_ip.Text = remote_ipend.Address.ToString(); subitem_hostname.Text = Encoding.UTF8.GetString(hostname_bytes); item.SubItems.Add(subitem_user); item.SubItems.Add(subitem_ip); item.SubItems.Add(subitem_hostname); item.Tag = (object)wawState.SignIn; //存储用户当前状态 UserlistUpdate(item, 1); UserCountUpdate(); //回复消息 MsgSend signinreply = new MsgSend(remote_ipend, wawCMD.WAW_SENDSTATE, frmMain.Userstate.ToString()); signinreply.Send(); signinreply.SendClose(); } catch (Exception ex) { MessageBox.Show(ex.Message, "消息接收处理WAW_BC_SIGNIN出错"); } break; case wawCMD.WAW_BC_SIGNOUT: try { ListViewItem item = new ListViewItem(); ListViewItem.ListViewSubItem subitem_user = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_ip = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_hostname = new ListViewItem.ListViewSubItem(); subitem_user.Text = Encoding.UTF8.GetString(user_bytes); subitem_ip.Text = remote_ipend.Address.ToString(); subitem_hostname.Text = Encoding.UTF8.GetString(hostname_bytes); item.SubItems.Add(subitem_user); item.SubItems.Add(subitem_ip); item.SubItems.Add(subitem_hostname); UserlistUpdate(item, 2); UserCountUpdate(); } catch (Exception ex) { MessageBox.Show(ex.Message, "消息接收处理WAW_BC_SIGNOUT出错"); } break; default: break; } } else//非广播消息处理 { switch (cmd) { case wawCMD.WAW_SENDSTATE: try { ListViewItem item = new ListViewItem(); ListViewItem.ListViewSubItem subitem_user = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_ip = new ListViewItem.ListViewSubItem(); ListViewItem.ListViewSubItem subitem_hostname = new ListViewItem.ListViewSubItem(); subitem_user.Text = Encoding.UTF8.GetString(user_bytes); subitem_ip.Text = remote_ipend.Address.ToString(); subitem_hostname.Text = Encoding.UTF8.GetString(hostname_bytes); item.SubItems.Add(subitem_user); item.SubItems.Add(subitem_ip); item.SubItems.Add(subitem_hostname); switch (Encoding.UTF8.GetString(message)) { case "SignIn": item.Tag = (object)wawState.SignIn; break; case "Absence": item.Tag = (object)wawState.Absence; break; case "Busy": item.Tag = (object)wawState.Busy; break; default: break; } UserlistUpdate(item, 1); UserCountUpdate(); } catch (Exception ex) { MessageBox.Show(ex.Message, "消息接收处理WAW_SENDSTATE出错"); } break; case wawCMD.WAW_GETNEWVER: break; case wawCMD.WAW_GETNEWVER_ACCEPT: break; case wawCMD.WAW_INPUTING: break; case wawCMD.WAW_MSGDISCARD: break; case wawCMD.WAW_MSGRECVD: break; case wawCMD.WAW_MULTICAST: break; case wawCMD.WAW_NOOPERATION: break; case wawCMD.WAW_SENDFILEDATA: break; case wawCMD.WAW_SENDFILEDATA_ACCEPT: break; case wawCMD.WAW_SENDFILEDATA_CANCEL: break; case wawCMD.WAW_SENDFILEDATA_REFUSE: break; case wawCMD.WAW_SENDFILEDATA_STOP: break; case wawCMD.WAW_SENDMSG: distri = new MsgDistribute(remote_ipend.Address.ToString(), user, hostname, msgstr); Thread threadDistribute = new Thread(new ThreadStart(distri.DistributeToChatForm)); threadDistribute.IsBackground = true; threadDistribute.Start(); break; default: break; } } }