解决方案 »

  1.   

    断点跟,看报错时的ip和port到底都是什么
      

  2.   

    每次connect并发送数据之后,close了吗
      

  3.   

    我这么写的
    using(var client=new tcpclient(ipaddress,port))
    {
    client.getstream().write(data);
    }
      

  4.   

    我这么写的
    using(var client=new tcpclient(ipaddress,port))
    {
    client.getstream().write(data);
    }
      

  5.   

    实际上当然没那么简单,后面有read,都在using大括号里面。
      

  6.   

    OK.
    下面是代码:
            public void Poll()
            {            
                foreach (var station in stations)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station);
                }
                if (WaitHandle.WaitAll(doneEvent.Values.ToArray(), millsecondsTimeout, false))
                {
                    int availabeThreads,aiot;
                    ThreadPool.GetAvailableThreads(out availabeThreads, out aiot);
                    Console.WriteLine("poll end" + index.ToString());
                }
                else
                {
                    Console.WriteLine("timeout");            }
            }
            public void ThreadPollCallback(object state)
            {
                var station = state as Station;
                try
                {                
                    ushort[] res = ReadWaveData(station.IP);
                    lock (lockObj)
                    {
                        threads[station.Id] = Thread.CurrentThread;
                        Data[station.Id] = new List<ushort>(res);
                        stationErrNum[station.Id] = 0;
                        Exceptions[station.Id] = null;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("comm thread error");
                    lock (lockObj)
                    {
                        threads[station.Id] = Thread.CurrentThread;
                        Exceptions[station.Id] = e;
                        stationErrNum[station.Id]++;
                        if (stationErrNum[station.Id] > 3)
                        {
                            Data[station.Id] = new List<ushort>(12); ;
                        }
                    }
                }
                finally
                {
                    doneEvent[station.Id].Set();
                }
            }
            public ushort[] ReadWaveData(string ipAddress)
            {
                ushort[] inputs;
                using (TcpClient client = TcpClientConnector.Connect(ipAddress, 3100, 500))
                {
                    var ns = client.GetStream();
                    var bw = new System.IO.BinaryWriter(ns);
                    bw.Write(COMMAND_READWAVEDATA, 0, 6);
                    bw.Flush();                
                }
                return inputs;
            }
      

  7.   

    OK.
    下面是代码:
            public void Poll()
            {            
                foreach (var station in stations)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station);
    .........你忘记贴你说的“抛出异常的那一行代码”了。
      

  8.   

    OK.
    下面是代码:
            public void Poll()
            {            
                foreach (var station in stations)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station);
    .........你忘记贴你说的“抛出异常的那一行代码”了。
    就是using (TcpClient client = TcpClientConnector.Connect(ipAddress, 3100, 500))这里。