Return data using BeginReceive to another method in different class

66 views Asked by At

I'm using the following code to receive a datagram asynchronously in a class named UDPComm. what I do is the following:

  1. I have another class named Manager which sends a message over the UDP connection UPComm.send(byte[])
  2. Then it calls UDPComm.startListening() to listen the message.

My problem is, how can I pass the received bytes in receive() method to Manager class? I tried to add a return but it give me an error in udp.BeginReceive(receive, new object()).

Your help is highly appreciated.

   private void startListening()
    {
        try
        {
            ar_ = udp.BeginReceive(receive, new object());
        }
        catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception in StartListening()" 
                                                   + e.ToString());
        }
    }


   `private void receive(IAsyncResult ar)
    {
        byte[] bytes = udp.EndReceive(ar, ref networkEndpointIp);
    }`
0

There are 0 answers