2014-11-04 101 views
0

面對問題:每40毫秒將相同的字節數組寫入流中。在無限循環中讀取此線程。但字節數組完全錯誤。這可能是混合流,任何人都面臨這個問題?字節C#混合流

寫入陣列:

public void HandleCommunication() 
    { 
     var dataBytes = new List<byte>(); 

     foreach (double value in _values) 
     { 
      dataBytes.AddRange(BitConverter.GetBytes(value)); 
     } 

     _client.GetStream().Write(dataBytes.ToArray(), 0, dataBytes.Count); 
     _client.GetStream().Flush(); 
    } 

和字節讀陣列:兩次後的字節數組來錯了。

public void GetData() 
    { 
     TcpClient client = _server.AcceptTcpClient(); 
     Console.WriteLine("Connected!"); 

     while (true) 
     { 
      var stream = client.GetStream(); 
      Values = new double[2 * 16 * 1024]; 
      byte[] buffer = new byte[Values.Length * sizeof(double)]; 

      stream.Read(buffer, 0, buffer.Length); 
      stream.Flush();    
     } 

當我讀數據每250毫秒的字節數組來正確!

好了,更多的細節: 我有numbers.I一個文本文件中讀取和發送:

public partial class MainWindow : Window 
{ 
    private double[] values; 
    private System.Timers.Timer timer = new System.Timers.Timer(40); 
    Client _client = new Client("127.0.0.1", 55443); 


    public MainWindow() 
    { 
     InitializeComponent(); 
     OpenSrcFile(); 

     timer.Elapsed += TimerOnElapsed; 
     timer.Enabled = true; 
    } 


    private void TimerOnElapsed(object sender, ElapsedEventArgs e) 
    { 
     _client.SendValues(values); 
    } 


    public void OpenSrcFile() 
    { 
     List<string> lines = File.ReadAllLines(@"file.txt").ToList(); 
     string r = lines[0]; 
     int sizeX = int.Parse(lines[1]); 
     lines.RemoveRange(0, 2); 
     values = GetValues(lines.ToArray()); 
    } 

}

客戶端代碼:

class Client 
{ 
    private TcpClient _client; 
    private Stream _stream; 
    private double[] _values; 


    public Client(String ipAddress, int portNum) 
    { 
     _client = new TcpClient() { SendBufferSize = 2 * 16 * 1024 * sizeof(double) }; 
     _client.Connect(ipAddress, portNum); 
     _stream = _client.GetStream(); 
    } 


    public void SendValues(double[] values) 
    { 
     _values = values; 
     HandleCommunication(); 
    } 


    public void HandleCommunication() 
    { 

     _isConnected = true; 

     var dataBytes = new List<byte>(); 

     foreach (double value in _values) 
     { 
      dataBytes.AddRange(BitConverter.GetBytes(value)); 
     } 

     NetworkStream newStream = _client.GetStream(); 
     _client.GetStream().Write(dataBytes.ToArray(), 0, dataBytes.Count); 
     _client.GetStream().Flush(); 

    } 

} 

的服務器然後接收這個字節數組,但我每次發送相同的數組。但是在兩次迭代後接受字節數組是不同的! Server代碼:

class Server 
{ 
    private TcpListener _server; 


    public Server(int port) 
    { 
     _server = new TcpListener(IPAddress.Parse("127.0.0.1"), port); 
     _server.Start(); 
    } 

    public double[] Values { get; private set; } 

    public delegate void DataUpdatedEventHandler(object sender, EventArgs args); 

    public event DataUpdatedEventHandler DataUpdated; 

    protected virtual void OnDataUpdated() 
    { 
     DataUpdatedEventHandler handler = DataUpdated; 
     if (handler != null) handler(this, EventArgs.Empty); 
    } 


    public void GetData() 
    { 


     // wait for client connection 

     TcpClient client = _server.AcceptTcpClient(); 

     Console.WriteLine("Connected!"); 



     while (true) 
     { 
      var stream = client.GetStream(); 

       Values = new double[2 * 16 * 1024]; 
       byte[] buffer = new byte[Values.Length * sizeof(double)]; 



       stream.Read(buffer, 0, buffer.Length); //wrong array of bytes! 



       for (int i = 0; i < Values.Length; i++) 
       { 
        Values[i] = BitConverter.ToDouble(buffer, i * sizeof(double)); 
       } 



       OnDataUpdated(); 

     } 
    } 
} 

我不明白它是什麼......

+0

檢查Write()和Read()的返回值。 – CodeCaster 2014-11-04 15:17:55

+0

爲什麼你在閱讀時沖洗()? – Sinatr 2014-11-04 15:28:37

+0

這個問題太含糊。請具體說明出了什麼問題,明確說明您預期會發生什麼,以及發生了什麼。此外,您應該發佈更完整的代碼示例,準確顯示您要發送的數據。總而言之,你的代碼的一個主要問題是你忽略了'stream.Read()'的返回值,所以如果你最初只接收到部分數據發送,你永遠不會發現。 – 2014-11-04 19:25:52

回答