Call thread does not support reading

I have a C # network application in which many anonymous users connect to the game service.

Now I check the logs, and sometimes 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 the NetworkStream object, now I'm trying to reproduce the problem, but how? How can I get this exception?

I tried to disconnect from myself, but it just gives a timeout, tried other things, but cannot make it work.

Maybe someone has an idea?

File contents:

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();
    }
}
+3
source share
1 answer

What arguments do you pass in

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

. NetworkStream.Length NetworkStream.Position.

i.e somthing ( )

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

, MSDN NetworkStream.Length NetworkStream.Position a NotSupportedException, .

+1

Source: https://habr.com/ru/post/1787297/


All Articles