我在程序中开启了一个定时器,1s钟接收一次      byte[] btData = new byte[5000];
      iReceive = localSocket.Receive(btData, 0, btData.Length, SocketFlags.None);但程序运行一段时间后,,就会开始接收相同的数据,例如
!AIVDM,1,1,,B,16:Qt60003`VPe8DbTi:5:fb0@=l,0*38
但是发送方仍然在发送着不同的数据。
请问是怎么回事!!
有人遇到过吗。

解决方案 »

  1.   


    public AisData(Int32 Port)
            {
                this._port = Port;
                localSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                ipRemoteEndPoint = new IPEndPoint(IPAddress.Any, this._port);
                remoteEndPoint = (EndPoint)ipRemoteEndPoint;
                localSocket.Blocking = false;
                localSocket.Bind(remoteEndPoint);
                localSocket.ReceiveTimeout = 100;
                objTime = new System.Threading.Timer(new TimerCallback(StartReceive), null, 0, 1000);
            }        private void StartReceive(object obj)
            {
                try
                {
                    int b = localSocket.Available;                if (b > 0)
                    {
                        byte[] btData = new byte[5000];
                        iReceive = localSocket.Receive(btData, 0, btData.Length, SocketFlags.None);                    if (iReceive > 0)
                        {
                            ReceiveData += Encoding.ASCII.GetString(btData);
                            if (this.GotOneSourceData != null)
                            {
                                this.GotOneSourceData(ReceiveData, _port);
                            }
                            //暂时在此处分析收到的数据                        ParserData(ReceiveData);                    }
                    }
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.TimedOut)
                    {
                    }
                    else
                    {
                        Console.WriteLine("False" + "  " + ex.Message);
                    }
                }        }
    AisData 这是个类 ,在类直接开启了一个定时器,1s读取一次数据,
    刚才在二楼回复的是说的是错的,我把发送的程序关掉了,却还能读到数据,是因为发送的程序100毫秒发一次,接收程序是1秒才读取一次缓冲区,发送端若发了10次,那接收程序也要收取10次,但需要10秒钟,难道socket 不能一次全读出来吗