调用流不支持读取

发布于 2024-10-14 03:59:47 字数 1847 浏览 8 评论 0原文

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

I have a c# network application where alot of anonymous users connect to (game service).

Now I check the logs and occasionally I see this exception:

[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

This error comes from a NetworkStream object, now I am trying to reproduce the problem, but how? How can I get this exception?

I tried disconnecting myself, but that just gives a timeout, tried other things, but cannot get it to work.

Maybe somebody has an idea?

Contents of the file is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-10-21 03:59:47

您在方法中传递什么参数

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 目前不支持。

What arguments are you passing in the

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

method. Are you using any of NetworkStream.Length or NetworkStream.Position properties.

i.e is it somthing like (not exactly)

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

then as explained in MSDN documentation use of NetworkStream.Length and NetworkStream.Position properties will always throw a NotSupportedException as its not currently Supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文