尝试使用c#通过TCP接收对象时获取异常

时间:2013-01-29 18:00:20

标签: c# serialization tcp

我正在尝试使用objectsTCPC#接收serialization。我不断收到objects,每个object都会被发送到新任务。我选择不使用threads,因为它太贵了。问题是,如果我一次只收到1 object一切都很顺利,但如果我试图收到超过1 object,几秒钟后我得到: "the input stream is not a valid binary format. the starting contents (in bytes) are: ..."

这是我的听力功能:

public void Listen()
        {
            try
            {
                TcpObject tcpObject = new TcpObject();
                IFormatter formatter = new BinaryFormatter();

                bool offline = true;
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(() => offline = Offline));

                while (!offline)
                {
                    tcpObject = (TcpObject)formatter.Deserialize(serverStream);

                    if (tcpObject.Command == Command.Transfer)
                    {
                        #region Task
                        Task.Factory.StartNew(() =>
                        {
                            SentAntenna sentAntenna = (SentAntenna)tcpObject.Object;

                            string antennaName = sentAntenna.Name;

                            if (MainWindow.SpectrumList.ContainsKey(antennaName))
                            {
                                PointCollection pointCollection = new PointCollection();
                                float minChan = sentAntenna.Min;
                                float maxChan = sentAntenna.Max;

                                if (MainWindow.SpectrumList[antennaName].spectrumViewModel.AbsoluteMinimum == -1)
                                {
                                    Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                        new Action(delegate
                                        {
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.AbsoluteMinimum = minChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.AbsoluteMaximum = maxChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.TBMinRange = minChan.ToString();
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.TBMaxRange = maxChan.ToString();
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.MinRange = minChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.MaxRange = maxChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.UpdateRange();
                                        }));
                                }

                                float gap = maxChan - minChan;
                                foreach (Frequency f in sentAntenna.Frequencies)
                                {
                                    float chan = ((f.Channel - minChan) / gap) * 310;
                                    float inten = ((f.Intensity - 1) / 599) * 100;
                                    pointCollection.Add(new Point(chan, inten));
                                }

                                pointCollection.Freeze();
                                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                    new Action(() => MainWindow.SpectrumList[antennaName].spectrumViewModel.AllAntennaPoints = pointCollection.Clone()));
                            }
                            Thread.Sleep(50);
                        });
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // raise an event
            }
        }

我做错了什么?

2 个答案:

答案 0 :(得分:0)

尝试移动

SentAntenna sentAntenna = (SentAntenna)tcpObject.Object;

到StartNew()之前的行。我相信这会解决你的问题。

我认为您不希望并发访问tcpObject,因为它对所有任务都是全局的。

或者你可以在while循环中实例化TcpObject,然后将它保持在每个任务的本地。

答案 1 :(得分:0)

此错误有几个原因:

  1. 当两个对象完全写入一个连接时

  2. 当serverStream出现问题时:流已收到部分数据或收到0长度数据

  3. 您可以完全访问tcpObject。这是一个坏主意。