c# - Beginreceive/Endreceive passing parameter -


i'm reading udp socket using following code, working well. need pass 'received' recv calling main. how can done.

   try         {           client.beginreceive(new asynccallback(recv), client);         }      private static void recv(iasyncresult res)     {          try         {             udpclient c = (udpclient)(res.asyncstate);              ipendpoint remoteipendpoint = new ipendpoint(ipaddress.any, 162);                        byte[]  received = c.endreceive(res, ref remoteipendpoint);             console.writeline(asciiencoding.ascii.getstring(received));              c.beginreceive(new asynccallback(recv), c);          }          catch (exception e)         {             console.writeline(e);          }     } 

in .net 4.5 can use async/await this:

using (var udpclient = new udpclient()) {     try     {         udpclient.connect(ipaddress.any, 162);         udpreceiveresult receiveasync = await udpclient.receiveasync();         console.writeline(encoding.ascii.getstring(receiveasync.buffer));     }     catch (exception e)     {         console.writeline(e);     } } 

Comments