Skip to content

The Dummy Programmer

Stories of daily programming

  • Home
  • The Dummy Programmer Robot
    • Overview
    • Version history & downloads
    • Tutorials
      • TDP Robot – The basics
      • A simple SQL Server backup solution
      • A simple SQL Server backup solution – A next step
  • My other dummy projects
  • Games
    • Space Y: An Asteroids clone
  • Services
    • What’s my user agent
    • What’s my IP address
  • About
  • Toggle search form

Reading data from a NetworkStream object in C#

Posted on October 10, 2020October 10, 2020 By

Good morning folks!

In this post I want to talk about a bug I discovered in my TDP.BaseServices library, which I remember you can download here. This bug prevented from properly deserializing objects I sent through the NetworkStream object.

The problem was in the way I read data from a NetworkStream object.

If you browse the library in the class “SocketHelper” you will find the following method (at least in the previous version…):

 private static byte[] ReceiveVarData(Stream stream)
 {
    int Recv;
    byte[] Datasize = new byte[4];
    Recv = stream.Read(Datasize, 0, Datasize.Length); 
    int Size = BitConverter.ToInt32(Datasize, 0);
    byte[] Data = new byte[Size];
    Recv = stream.Read(Data, 0, Size);
           
    return Data;
 }

What’s wrong with it? The problem here is that the NetworkStream.Read method doesn’t guarantee that it will read the number of bytes you specify in the third parameter (the variable “Size” in the code above). So you always have to check the return value of the Read method, which returns the exact number of bytes read. If this number is less than the number you need to read, you have to call the Read method again to read the remaining part.

Since I had to make this fix, I took the opportunity to slightly modify the entire class, because I realized that the way I was sending and receiving the size of the message could suffer of the same problem.

If you download the new version of the library you should understand what I mean…

I apologize to the millions of developers who are using my library 🤣.

C#, Sockets

Post navigation

Previous Post: Configure a PHP development environment in Windows (part 4 of 4)
Next Post: System.Net.Sockets.SocketException (0x80004005): No such host is known

Related Posts

Working with C# async / await keywords (part 2 of 3) C#
Could not load file or assembly ‘DocumentFormat.OpenXml, Version=2.7.2.0 C#
Working with C# async / await keywords (part 3 of 3) C#
Push notifications library with C# and .NET Framework C#
Capture screen using C# C#
Working with C# async / await keywords (part 1 of 3) C#

Search

Related posts

  • Load a huge amount of data in a Sql Server table
  • Sql Server error: Cannot construct data type date, some of…

Categories

  • .NET 6 (1)
  • AJAX (1)
  • Android (2)
  • Apache (4)
  • ASP.NET (9)
  • ASP.NET MVC (3)
  • Avalonia UI (1)
  • BCP (1)
  • Bitlocker (2)
  • C# (14)
  • CentOS (4)
  • ClosedXML (1)
  • CLR (1)
  • DNS (1)
  • Encryption (3)
  • Excel (2)
  • FuelPHP (3)
  • Games (2)
  • Google Chrome (1)
  • GSuite (1)
  • HTML (1)
  • Imagick (2)
  • Javascript (1)
  • Kindle (1)
  • LAMP (3)
  • Linux (6)
  • MariaDB (2)
  • Mathematics (2)
  • MySql (4)
  • NPOI (1)
  • Office 365 (1)
  • Perl (1)
  • PHP (6)
  • Programming (1)
  • Remote desktop (1)
  • SFTP (2)
  • Sockets (3)
  • Sql Server (18)
  • Sql Server 2016 (14)
  • Sql Server 2019 (1)
  • SSL (2)
  • Task scheduler (1)
  • Telerik ASP.NET AJAX (2)
  • The Dummy Programmer Chat (2)
  • The Dummy Programmer Robot (6)
  • Threading (5)
  • Tools (1)
  • TPL (3)
  • TypeScript (3)
  • Ubuntu (4)
  • Virtualization software (3)
  • Visual Studio (1)
  • Visual Studio Code (2)
  • Web fonts (1)
  • Web programming (6)
  • Windows (12)
  • Windows 10 (15)
  • Windows Forms (1)
  • Windows Server (6)

Copyright © 2023 The Dummy Programmer | Privacy Policy | Terms of use |

Powered by PressBook Masonry Dark