/// <summary> /// 侦听UDPListen /// </summary> private void UDPListen() { byte[] data = new byte[102400]; while (RunningFlag) { if (mySocket == null || mySocket.Available < 1) { Thread.Sleep(200); continue; } int rlen = mySocket.ReceiveFrom(data, ref RemotePoint); var d = new System.IO.MemoryStream(data); Message = hs.DeSerializeBinary <Helper_Message>((d)) as Helper_Message; if (Message != null) { if (Message.IsColose) { RunningFlag = false; } UDPOnRecived(); } } }
/// <summary> /// UDP发送 /// </summary> /// <param name="name"></param> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="message"></param> /// <returns></returns> public bool UDPSend(Helper_Message message) { message.msgID = Guid.NewGuid().ToString(); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(message.RIP), int.Parse(message.RPort)); if (message.Data.Length <= 1024) { try { var data = hs.SerializeBinary(message).ToArray(); mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); mySocket.SendTo(data, data.Length, SocketFlags.None, (EndPoint)(ipep)); return(true); } catch { return(false); } } else { #region start Helper_Message start = new Helper_Message(); start.sendKind = message.sendKind; start.sendState = SendState.Start; start.SName = message.SName; start.SIP = message.SIP; start.SPort = message.SPort; start.RIP = message.SIP; start.RPort = message.RPort; start.Data = Encoding.Unicode.GetBytes(message.msgFileName); var startData = Helper_Serializers.Instance.SerializeBinary(start).ToArray(); mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); mySocket.SendTo(startData, startData.Length, SocketFlags.None, (EndPoint)(ipep)); #endregion #region Sending Helper_Message sending = new Helper_Message(); sending.sendKind = message.sendKind; sending.sendState = SendState.Sending; sending.SName = message.SName; sending.SIP = message.SIP; sending.SPort = message.SPort; sending.RIP = message.RIP; sending.RPort = message.RPort; sending.Data = Encoding.Unicode.GetBytes(""); MemoryStream stream = new MemoryStream(message.Data); int sendlen = 1024; long sunlen = (stream.Length); int offset = 0; while (sunlen > 0) { sendlen = 1024; if (sunlen <= sendlen) { sendlen = Convert.ToInt32(sunlen); } byte[] msgdata = new byte[sendlen]; stream.Read(msgdata, offset, sendlen); sending.sendState = SendState.Sending; sending.Data = msgdata; var sendingData = Helper_Serializers.Instance.SerializeBinary(sending).ToArray(); mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); mySocket.SendTo(sendingData, sendingData.Length, SocketFlags.None, (EndPoint)(ipep)); sunlen = sunlen - sendlen; } #endregion #region end Helper_Message end = new Helper_Message(); end.sendKind = message.sendKind; end.sendState = SendState.End; end.SName = message.SName; end.SIP = message.SIP; end.SPort = message.SPort; end.RIP = message.RIP; end.RPort = message.RPort; end.Data = Encoding.Unicode.GetBytes(message.msgFileLeiXing); stream.Close(); var endData = Helper_Serializers.Instance.SerializeBinary(end).ToArray(); mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); mySocket.SendTo(endData, endData.Length, SocketFlags.None, (EndPoint)(ipep)); #endregion return(true); } }