我有一个c#网络应用程序,其中有很多匿名用户连接到(游戏服务)。
现在我查看日志,偶尔也会看到这个例外:
[10:30:18.21352] System.Int32 Read(Byte[], Int32, Int32): The stream does not support reading.
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at BusinessLayer.Listener.ListenerWorker.ProcessClient(Object obj) in File.cs:line 141
此错误来自NetworkStream对象,现在我正在尝试重现该问题,但是如何?我怎么能得到这个例外?
我尝试断开自己的连接,但这只是暂停,尝试了其他的事情,但无法让它工作。
也许有人有想法?
该文件的内容是:
private static void ProcessClient(
Object obj)
{
ISession session = (ISession)obj;
NetworkStream networkStream = null;
try
{
DebugUtility.SetThreadName("Worker: {0}", session.Name);
networkStream = session.TcpClient.GetStream();
networkStream.ReadTimeout = Config.ReadTimeout;
// Loop received packets (blocks untill next packet)
Int32 packetSize;
Byte[] buffer = new Byte[session.PacketSize];
while ((packetSize = networkStream.Read(buffer, 0, buffer.Length)) != 0)
{
// Get String from packet bytes
String packet = Encoding.UTF8.GetString(buffer, 0, packetSize);
// Check if packet has data
if (String.IsNullOrEmpty(packet))
continue;
// Log biggest received package
DebugUtility.CheckMaxPacketSize(session.Name, packet.Length);
// Handle packet (in new thread)
Logger.DebugLog("Received: {0}", packet);
ThreadPool.QueueUserWorkItem(session.HandlePacket, packet);
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
finally
{
if (networkStream != null)
networkStream.Close();
if (session != null)
session.Disconnect();
}
}
答案 0 :(得分:1)
你在
中传递了什么参数System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
方法。您使用任何 NetworkStream.Length 或 NetworkStream.Position 属性。
即它是什么样的(不完全是)
System.Net.Sockets.NetworkStream.Read(buffer, stream.Position, stream.Length)
然后如MSDN文档中所述使用 NetworkStream.Length
和NetworkStream.Position
属性将始终抛出一个NotSupportedException
,因为它当前不是支撑。