/// <summary> /// 发送广播消息 /// </summary> /// <param name="cmd">消息命令字类型</param> public void BoardCast(wawCMD cmd) { List <byte> bytesource = new List <byte>(); 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)) { bytesource.AddRange(PreDefine.DataHeadPreProcess(cmd)); byte[] sendbuff = bytesource.ToArray(); udpclient.Send(sendbuff, sendbuff.Length, boardcast_ipport); } }
/// <summary> /// /// </summary> /// <param name="ipendpoint"></param> /// <param name="txt">要发送的消息文本</param> public MsgSend(IPEndPoint ipendpoint, wawCMD cmd, string txt) { remote_ipport = ipendpoint; List <byte> bytesource = new List <byte>(); bytesource.AddRange(PreDefine.DataHeadPreProcess(cmd)); bytesource.AddRange(Encoding.UTF8.GetBytes(txt)); sendbuff = bytesource.ToArray(); //也可使用如下方法进行byte数组拼接,一般来讲,对于大byte数组,采用泛型效率要高很多 //byte[] sendbuf = PreDefine.DataHeadPreProcess(cmd).Concat(Encoding.UTF8.GetBytes(txt)).ToArray(); }
public const int WM_COPYDATA = 0x004A; //Windows消息常量值:当一个应用程序传递数据给另一个应用程序时发送此消息 /// <summary> /// 数据报头部封装 /// </summary> /// <returns>封装后的字节数组</returns> public static byte[] DataHeadPreProcess(wawCMD cmd) { byte[] datehead_bytes = new byte[128]; try { //封装版本号 byte[] ver_bytes = new byte[16]; byte[] temp = new byte[50];//用于处理用户名、主机名的临时字节数组 byte[] vermajor_bytes = BitConverter.GetBytes(InfoSet.Ver.Major); byte[] verminor_bytes = BitConverter.GetBytes(InfoSet.Ver.Minor); byte[] verbuild_bytes = BitConverter.GetBytes(InfoSet.Ver.Build); byte[] verrevision_bytes = BitConverter.GetBytes(InfoSet.Ver.Revision); Array.Copy(vermajor_bytes, 0, ver_bytes, 0, 4); Array.Copy(verminor_bytes, 0, ver_bytes, 4, 4); Array.Copy(verbuild_bytes, 0, ver_bytes, 8, 4); Array.Copy(verrevision_bytes, 0, ver_bytes, 12, 4); //封装包编号 byte[] serialnum_bytes = new byte[8]; Array.Copy(BitConverter.GetBytes(DateTime.Now.ToFileTimeUtc()), serialnum_bytes, 8); //封装用户名 byte[] user_bytes = Enumerable.Repeat((byte)0x0, 50).ToArray();//将用户名数组全部填'\0' Array.Copy(Encoding.UTF8.GetBytes(InfoSet.User), user_bytes, Encoding.UTF8.GetBytes(InfoSet.User).Length < 50? Encoding.UTF8.GetBytes(InfoSet.User).Length:50); //封装主机名 byte[] hostname_bytes = Enumerable.Repeat((byte)0x0, 50).ToArray();//将主机名数组全部填'\0' Array.Copy(Encoding.UTF8.GetBytes(InfoSet.HostName), hostname_bytes, Encoding.UTF8.GetBytes(InfoSet.HostName).Length < 50? Encoding.UTF8.GetBytes(InfoSet.HostName).Length:50); //封装命令编号 byte[] cmd_bytes = new byte[4]; Array.Copy(BitConverter.GetBytes((uint)cmd), cmd_bytes, 4); Array.Copy(ver_bytes, 0, datehead_bytes, 0, 16); Array.Copy(serialnum_bytes, 0, datehead_bytes, 16, 8); Array.Copy(user_bytes, 0, datehead_bytes, 24, 50); Array.Copy(hostname_bytes, 0, datehead_bytes, 74, 50); Array.Copy(cmd_bytes, 0, datehead_bytes, 124, 4); } catch (Exception ex) { MessageBox.Show(ex.Message, "数据报头部封装出错"); } return(datehead_bytes); }
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; } } }