0

I have currently been trying to go over some code on GitHub for a UDP DNS Server. The DNS Server is meant to allow for proxy-like MITM behaviour so I can look at the, redirect or block the DNS packets. Reading through the code I saw two functions that look very similar but I can't quite grasp the difference between the two. I have tried to look through the Microsoft documentation but that had not helped me understand the difference either. This is the extract of code I am referring to. The project in question that I am reading through can be found here: Portable DNS Proxy

public byte[] directRead()
        {            
            byte[] msg = client.Receive(ref ep);
            return msg;
        }

public void read()
        {
            Console.WriteLine("Initializing Read");
            client.BeginReceive(new AsyncCallback(recvAsync), null);
            Console.WriteLine("Read async started");
        }

In this instance the variable named client is simply an instance of the UdpClient class and the variable ep is the instance of the IPEndPoint class.

Zik
  • 101
  • 3
  • 1
    client.Receive will block (code execution stops until something is received, and continues from the next line when it does), and BeginReceive won't, and the received message should be handled by the asynchronous callback, which is recvAsync in yhe source code above. – Oguz Ozgul Apr 26 '20 at 16:59
  • The MSDN pages for both of those methods includes examples. Have you grok the concept of asynchronous? – Martheen Apr 26 '20 at 17:03
  • so BeginRecieve will just constantly wait for a message and if it gets one it doesn't stop it just keeps accepting messages or reads? is there any advantage to be doing this using async rather than just using client.Recieve on a loop on a seperate thread? and no, I am currently just starting out trying to grasp these concepts by putting them into practice. I know the theory behind them but not their application. – Zik Apr 26 '20 at 17:08
  • You can read here: http://www.interact-sw.co.uk/iangblog/2004/09/23/threadless – Oguz Ozgul Apr 26 '20 at 17:38
  • thank you so much :) – Zik Apr 26 '20 at 17:55

0 Answers0