Question

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();
    }
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top