“无法从传输连接中读取数据。”请大家帮忙,谢谢。
我的代码如下,请大家帮忙看看。
发送数据代码:
//
//将信息队列发送
//
public static void Send(ArrayList array, Queue queue, bool validate)
{
MemoryStream sendStream = GetSendStream(array, validate);
sendStream.Position = 0;
Byte[] sendBytes = new Byte[sendStream.Length];
sendStream.Read(sendBytes, 0, (int)sendStream.Length);
queue.Enqueue(sendBytes);
}
//
//将用户信息写进一个队列
//
public void Login()
{
ArrayList message = new ArrayList();
message.Add(formMain.userInfo);
message.Add(userPosition);
Send(message, sendQueue, true);
}
//
//将接收到的信息进行再分配处理
//
public static ArrayList ReceiveScheduler(TcpClient client, NetworkStream stream)
{
byte[] bytes = new byte[client.ReceiveBufferSize];
ArrayList message = null;
int bytesRead = stream.Read(bytes, 0, (int) client.ReceiveBufferSize); // 提示这行出错。
if (bytesRead > 0)// 图过信息队列中,信息内容不为空,则读出
{
try
{
int sendLength = BitConverter.ToInt32(bytes, 1) + 9;
if (sendLength == bytesRead)
{
MemoryStream mStream = new MemoryStream(bytes, 9, sendLength - 9);
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
message = (ArrayList) formatter.Deserialize(mStream);
message.Add(BitConverter.ToUInt32(bytes, 5));
}
}
catch
{
//
// 忽略
//
}
}
return message;
}
断点调试的时候,错误出现在这一行:
int bytesRead = stream.Read(bytes, 0, (int) client.ReceiveBufferSize);
错误提示为:“无法从传输连接中读取数据”
之前在传输其它数据的时候,数据大小大约为四、五百这样,没有出现这个错误;但是在传了某一个信息队列,大概300多的时候就出现这个错误了。
请问各位,这个错误一般是因为什么原因导致产生的呢?应该如何解决?或者说在实际应用中应该如何避免呢?请大家帮帮忙,谢谢。