UdpClient.BeginReceive callback function??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tenacious

    #1

    UdpClient.BeginReceive callback function??

    MSDN gives this code as an example to create a callback function for
    UdpClient.Begin Receive. It doesn't compile because the compiler does
    not recognize "UdpState". I can't find any info on it either. Does
    anyone know what "UdpState" is or could you point me to some sample
    code on how to properly implement a ReceiveCallback function? Any help
    would be appreciated.

    public static bool messageReceived = false;
    public static void ReceiveCallback (IAsyncResult ar)
    {
    UdpClient u = (UdpClient)((Ud pState)(ar.Asyn cState)).u;
    IPEndPoint e = (IPEndPoint)((U dpState)(ar.Asy ncState)).e;

    Byte[] receiveBytes = u.EndReceive(ar , ref e);
    string receiveString = Encoding.ASCII. GetString(recei veBytes);

    Console.WriteLi ne("Received: {0}", receiveString);
    messageReceived = true;
    }

    MSDN link:
    ms-help://MS.MSDN.vAug06. en/cpref10/html/M_System_Net_So ckets_UdpClient _BeginReceive_1 _2bb042b2.htm

  • Patrick Steele

    #2
    Re: UdpClient.Begin Receive callback function??

    In article <1159919356.808 658.88110@b28g2 000cwb.googlegr oups.com>,
    CT_Taylor@yahoo .com says...
    MSDN gives this code as an example to create a callback function for
    UdpClient.Begin Receive. It doesn't compile because the compiler does
    not recognize "UdpState". I can't find any info on it either. Does
    anyone know what "UdpState" is or could you point me to some sample
    code on how to properly implement a ReceiveCallback function? Any help
    would be appreciated.
    >
    public static bool messageReceived = false;
    public static void ReceiveCallback (IAsyncResult ar)
    {
    UdpClient u = (UdpClient)((Ud pState)(ar.Asyn cState)).u;
    IPEndPoint e = (IPEndPoint)((U dpState)(ar.Asy ncState)).e;
    >
    Byte[] receiveBytes = u.EndReceive(ar , ref e);
    string receiveString = Encoding.ASCII. GetString(recei veBytes);
    >
    Console.WriteLi ne("Received: {0}", receiveString);
    messageReceived = true;
    }
    >
    MSDN link:
    ms-help://MS.MSDN.vAug06. en/cpref10/html/M_System_Net_So ckets_UdpClient _BeginReceive_1 _2bb042b2.htm
    Looks like they missed part of the code. My guess is that it's a simple
    class to maintain some state in the Async call. Probably:

    public class UdpState
    {
    public UdpClient u;
    public IPEndPoint e;
    }

    --
    Patrick Steele

    Comment

    Working...