Example #1
0
        //接收数据的回调函数
        public void ReceiveCallback(IAsyncResult param)
        {
            if (param.IsCompleted)
            {
                UdpPacket udpp = null;
                try
                {
                    byte[] data = client.EndReceive(param, ref remotIpEnd);//接收数据,当Client端连接主机的时候,test就变成Cilent端的IP了
                    udpp = (UdpPacket)SerializationUnit.DeserializeObject(data);
                }
                catch (Exception ex)
                {
                    //异常处理操作
                }
                finally
                {
                    AsyncReceiveData();
                }
                if (udpp != null)//触发数据包收到事件
                {
                    if (!RecListDic.ContainsKey(udpp.sequence))
                    {
                        RecListDic.Add(udpp.sequence, new RecDataList(udpp));
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    else
                    {
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    foreach (KeyValuePair <long, RecDataList> ss in RecListDic)//循环看看是否有哪一个满了
                    {
                        Msg m = ss.Value.show();
                        if (m != null)
                        {
                            if (ReceivedOk != null)
                            {
                                ReceivedOk(m, remotIpEnd);
                            }
                            RecListDic.Remove(ss.Value.sequence);
                            break;
                        }
                    }

                    PackageEventArgs arg = new PackageEventArgs(udpp, remotIpEnd);
                    OnPackageReceived(arg);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 同步数据接收方法
        /// </summary>
        public void ReceiveData()
        {
            while (true)
            {
                IPEndPoint retip = null;
                UdpPacket  udpp  = null;
                try
                {
                    byte[] data = client.Receive(ref retip);//接收数据,当Client端连接主机的时候,retip就变成Cilent端的IP了
                    udpp = (UdpPacket)SerializationUnit.DeserializeObject(data);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    //异常处理操作
                }
                if (udpp != null)
                {
                    if (!RecListDic.ContainsKey(udpp.sequence))
                    {
                        RecListDic.Add(udpp.sequence, new RecDataList(udpp));
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    else
                    {
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    foreach (KeyValuePair <long, RecDataList> ss in RecListDic)//循环看看是否有哪一个满了
                    {
                        Msg m = ss.Value.show();
                        if (m != null)
                        {
                            if (ReceivedOk != null)
                            {
                                ReceivedOk(m, retip);
                            }
                            RecListDic.Remove(ss.Value.sequence);
                            break;
                        }
                    }

                    PackageEventArgs arg = new PackageEventArgs(udpp, retip);
                    OnPackageReceived(arg);//数据包收到触发事件
                }
            }
        }