I'm using the following code to receive a datagram asynchronously in a class named UDPComm.
what I do is the following:
- I have another class named
Managerwhich sends a message over the UDP connectionUPComm.send(byte[]) - 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);
}`