面临的问题是:每40毫秒向流写入相同的字节数组。在无限循环中读取此线程。但是字节数组完全错了。这可能是混合流,有人面临这个问题吗?
写入字节数组:
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毫秒读取一次数据时,字节数组就会正确出现
好的,更多详细信息:我有一个带数字的文本文件。我读了它并发送:
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();
}
}
然后服务器接收这个字节数组,但我每次都发送相同的数组。但是在两次迭代之后,字节数组的接受度是不同的!服务器代码:
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();
}
}
}
我不明白它是什么…
看看MS关于TcpClient类的文章:http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream%28v=vs.110%29.aspx
正如您所看到的,.Net中的流可以具有可变的字节长度。您应该使用获取数据
Encoding.UTF8.GetString (bytes);