From: Tenacious on
MSDN gives this code as an example to create a callback function for
UdpClient.BeginReceive. 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)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

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

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

MSDN link:
ms-help://MS.MSDN.vAug06.en/cpref10/html/M_System_Net_Sockets_UdpClient_BeginReceive_1_2bb042b2.htm

From: Patrick Steele on
In article <1159919356.808658.88110(a)b28g2000cwb.googlegroups.com>,
CT_Taylor(a)yahoo.com says...
> MSDN gives this code as an example to create a callback function for
> UdpClient.BeginReceive. 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)((UdpState)(ar.AsyncState)).u;
> IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
>
> Byte[] receiveBytes = u.EndReceive(ar, ref e);
> string receiveString = Encoding.ASCII.GetString(receiveBytes);
>
> Console.WriteLine("Received: {0}", receiveString);
> messageReceived = true;
> }
>
> MSDN link:
> ms-help://MS.MSDN.vAug06.en/cpref10/html/M_System_Net_Sockets_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
http://weblogs.asp.net/psteele