UdpClient Class exsample appears to eat memory until it stops

Jan Flikweert 21 Reputation points
2022-09-16T10:47:02.863+00:00

I modified the example posted in doc UdpClient Class so it can operate in a loop. It runs a while and then it stops while I need it to run forever.
The code to reproduce:

/**********************************************************************************************************************************  

// See https://aka.ms/new-console-template for more information  
//public class UdpClient: IDisposable;  
  
global using System.Net;  
global using System.Net.Sockets;  
global using System.Text;   
  
void test()  
{  
        UdpClient udpClient = new UdpClient(8888);  
        udpClient.Connect("192.168.203.18", 8888);  
        IPEndPoint RemoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);  
        Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there today?");  
        udpClient.Send(sendBytes, sendBytes.Length);  
        Console.WriteLine("-send completed");  
        Byte[] receiveBytes = udpClient.Receive(ref RemoteIPEndPoint);  
         udpClient.Close();  
}  
   
Console.WriteLine("Hello, World!");  
    while (true) test();  
  
**********************************************************************************************************************\  
  

The other side reply's with "acknowledge". (Wemos D1 Arduino)
The debugger shows a constant increase in memory until it stops.
Note I am on an up-to-date windows 11 system. Running visual studio. And it is the first time I use : ".NET6"
Am I doing something wrong?
Thanks for the answer.
Jan

.NET Internet of things
.NET Internet of things
.NET: Microsoft Technologies based on the .NET software framework.Internet of things: A concept that aims to extend the benefits of the regular internet, including constant connectivity, remote control ability, and data sharing, to goods in the physical world.
30 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sander van de Velde | MVP 32,811 Reputation points MVP
    2022-09-18T21:16:36.297+00:00

    Hello @Jan Flikweert ,

    working with UDP can be challenging.

    It seems you tried the 'blocking' version of working with UDP.

    Have you tried the async version also?

    I made a little adjustment to my project by introducing a Boolean and waiting for the next message using a while loop until the current is handled.

    This runs stable in our current project.

    MS Learn was not accepting my code samples...

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Jan Flikweert 21 Reputation points
    2022-09-20T13:59:08.897+00:00

    Hello Sander,
    Thanks for the reply.
    No, I did not try the async version. I did not even know that I was using a blocking version. I just used the example on the MS website and modified it so it would fit my purpose.
    I will try it and let you know.
    Again thanks for the response.
    Jan

    0 comments No comments

  2. Jan Flikweert 21 Reputation points
    2022-09-20T19:47:15.223+00:00

    Hello Sander,
    Again, thanks for the effort. To really solve it I need more time. I don't have that today, but I have a good working tcp version.
    (My Async version did not work for a long time. Probably the same kind of problems. Probably something to do with dispose().)

    Again thanks for the idea.
    Jan
    I close this issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.