我试图使用C#和System.Net.Sockets类为我的游戏编写网络部分。
我做错了什么,如何才能避免这种错误的数据流?
我是否只是在1毫秒内发送了太多的数据,并且需要在一段时间内发送?
这是发送的信息:
TcpClient client;
public void SendData(byte[] b)
{
//Try to send the data. If an exception is thrown, disconnect the client
try
{
lock (client.GetStream())
{
client.GetStream().BeginWrite(b, 0, b.Length, null, null);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}这是接收到的信息:
byte[] readBuffer;
int byfferSize = 2048;
private void StartListening()
{
client.GetStream().BeginRead(readBuffer, 0, bufferSize, StreamReceived, null);
}
private void StreamReceived(IAsyncResult ar)
{
int bytesRead = 0;
try
{
lock (client.GetStream())
{
bytesRead = client.GetStream().EndRead(ar); // просмотр длины сообщения
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
//An error happened that created bad data
if (bytesRead == 0)
{
Disconnect();
return;
}
//Create the byte array with the number of bytes read
byte[] data = new byte[bytesRead];
//Populate the array
for (int i = 0; i < bytesRead; i++)
data[i] = readBuffer[i];
//Listen for new data
StartListening();
//Call all delegates
if (DataReceived != null)
DataReceived(this, data);
}它是主要的网络代码。
发布于 2013-07-25 16:41:57
我不知道在您收到数据之后如何处理这些数据,但是很可能您没有从连接中读取所有的数据。你有:
bytesRead = client.GetStream().EndRead(ar);无法保证所读取的字节数是服务器发送的所有字节。例如,服务器可以发送2,048字节,但是当您调用Read时,只有1,024字节可用。其余的还在“中转”。正如NetworkStream.Read文档所说:
读取操作读取尽可能多的数据,最多读取由size参数指定的字节数。
你可能会收到部分数据包。如果您的DataReceived处理程序假定data缓冲区包含完整的数据包,那么您将遇到问题。
要从网络流可靠地读取数据,您需要知道应该读取多少数据,或者需要一个记录分隔符。有些东西必须确保,如果你期待一个完整的包,你得到一个完整的包之前,你试图处理它。您的代码只是检查是否bytesRead不是0。如果是别的什么,你就把它传下去。这将是一个问题,除非您的DataReceived处理程序知道如何缓冲部分数据包。
另外,您实际上不需要锁定网络流。除非您可以从同一流中读取多个线程。那将是灾难性的。把锁扔了。你不需要它。
https://stackoverflow.com/questions/17862843
复制相似问题