/// <summary> /// Send UDP private message to a specific recipient /// </summary> /// <param name="txtMsg">UDPDatagram private message</param> /// <param name="epRemote">Recipient address</param> public void SendMessage(UdpDatagram txtMsg, IPEndPoint epRemote) { byte[] data = new byte[soc.SendBufferSize]; string txtMsgStr = txtMsg.ToString(); int leftLen = txtMsgStr.Length; int sendLen = 0; try { while (leftLen != 0) { if (leftLen >= soc.SendBufferSize) { data = Encoding.UTF8.GetBytes(txtMsgStr.Substring(sendLen, soc.SendBufferSize)); soc.SendTo(data, soc.SendBufferSize, SocketFlags.None, epRemote); leftLen -= soc.SendBufferSize; sendLen += soc.SendBufferSize; } else { data = Encoding.UTF8.GetBytes(txtMsgStr.Substring(sendLen, leftLen)); soc.SendTo(data, leftLen, SocketFlags.None, epRemote); leftLen = 0; } } } catch (Exception ex) { if (SendFail != null) { SendFail(String.Format("Message Send Failed! " + ex.Message)); } } }
/// <summary> /// Convert the network string to UdpDatagram /// </summary> /// <param name="netstr">Received network string</param> /// <returns>UdpDatagram Object</returns> public static UdpDatagram Convert(string netstr) { UdpDatagram data = new UdpDatagram(); int ChCount = 0; int netstrLen = netstr.Length; int headfieldcount = 1; string typestr = ""; string fromAddrstr = ""; string toAddrstr = ""; string hostnamestr = ""; string hostnameLenstr = ""; int hostnameLen = 0; string messagestr = ""; for (; ChCount < netstrLen && headfieldcount != 6; ChCount++) //Extract all the headerfields { if (headfieldcount != 6 && netstr[ChCount] == ',') { if (headfieldcount == 4) { hostnameLen = int.Parse(hostnameLenstr); } headfieldcount++; } else if (netstr[ChCount] != ',') { switch (headfieldcount) { case 1: typestr += netstr[ChCount]; break; case 2: fromAddrstr += netstr[ChCount]; break; case 3: toAddrstr += netstr[ChCount]; break; case 4: hostnameLenstr += netstr[ChCount]; break; case 5: hostnamestr = netstr.Substring(ChCount, hostnameLen); ChCount += hostnameLen - 1; break; } } } if (ChCount < netstrLen) { messagestr = netstr.Substring(ChCount); } data.Type = (UdpDatagramType)Enum.Parse(typeof(UdpDatagramType), typestr); data.FromAddress = fromAddrstr; data.ToAddress = toAddrstr; data.HostName = hostnamestr; data.Message = messagestr; return(data); }
/// <summary> /// Receive UDP message and call the corresponding event handler /// </summary> private void ListenIncomingMsg() { byte[] buffer = new byte[soc.ReceiveBufferSize]; EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); try { while (true) { int len = soc.ReceiveFrom(buffer, ref remoteEP); UdpDatagram msg = UdpDatagram.Convert(Encoding.UTF8.GetString(buffer, 0, len)); switch (msg.Type) { case UdpDatagramType.Chat: RemoteMessageReceived(msg.HostName, IPAddress.Parse(msg.FromAddress), msg.Message); break; case UdpDatagramType.OnLine: RemoteOnLine(msg.HostName, IPAddress.Parse(msg.FromAddress)); break; case UdpDatagramType.OffLine: RemoteOffLine(msg.HostName, IPAddress.Parse(msg.FromAddress)); break; case UdpDatagramType.FAccept: FileAccept(msg.HostName, msg.Message, IPAddress.Parse(msg.FromAddress)); break; case UdpDatagramType.FRefuse: FileRefuse(msg.HostName, msg.Message, IPAddress.Parse(msg.FromAddress)); break; case UdpDatagramType.FSendReq: FileSendReq(msg.HostName, msg.Message, IPAddress.Parse(msg.FromAddress)); break; } } } catch (SocketException) { } catch (ThreadAbortException) { } catch (Exception e) { MessageBox.Show(String.Format("Error Type:{0};Message:{1}", e.GetType(), e.Message)); } }
/// <summary> /// Send UDP group message to all users /// </summary> /// <param name="Msg">UDPDatagram group message</param> /// <param name="groupEP">Multicast group address</param> public void MultiCast(UdpDatagram Msg, IPEndPoint groupEP) { byte[] msg = Encoding.UTF8.GetBytes(Msg.ToString()); soc.SendTo(msg, groupEP); }