gpt4 book ai didi

c# - Invoke 流不支持读取

转载 作者:行者123 更新时间:2023-11-30 18:06:31 25 4
gpt4 key购买 nike

我有一个 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 对象,现在我正在尝试重现该问题,但如何重现?我怎样才能得到这个异常(exception)?

我尝试断开自己的连接,但这只是超时,尝试了其他方法,但无法正常工作。

也许有人有想法?

文件内容为:

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();
}
}

最佳答案

你在传递什么参数

System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

方法。您是否正在使用 任何 NetworkStream.LengthNetworkStream.Position 属性。

也就是说(不完全是)

System.Net.Sockets.NetworkStream.Read(buffer, stream.Position, stream.Length)

然后如 MSDN 文档中所述使用 NetworkStream.LengthNetworkStream.Position属性将始终抛出一个NotSupportedException,因为它当前不受支持。

关于c# - Invoke 流不支持读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4774680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com