日期:2014-05-17  浏览次数:21094 次

MSDN中的示例,使用 BeginReceive 异步接收服务器响应,编译时出现找不到类型或命名空间名称“UdpState”,应该怎么修改?
//下面的代码是MSDN中的示例,使用 BeginReceive 异步接收服务器响应。为什么我编译时,告诉我
//错误 1 找不到类型或命名空间名称“UdpState”(是否缺少 using 指令或程序集引用?)
//应该怎么修改,谢谢!


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;
}

public static void ReceiveMessages()
{
  // Receive a message and write it to the console.
  IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
  UdpClient u = new UdpClient(e);

  UdpState s = new UdpState();
  s.e = e;
  s.u = u;

  Console.WriteLine("listening for messages");
  u.BeginReceive(new AsyncCallback(ReceiveCallback), s);

  // Do some work while we wait for a message. For this example,
  // we'll just sleep
  while (!messageReceived)
  {
  Thread.Sleep(100);
  }
}

------解决方案--------------------
缺少程序集的引用 找到这个程序集的DLL 添加到引用里面
------解决方案--------------------
探讨
看老外的论坛,说要自己定义一个
class UdpState
{
public UdpClient u;
public IPEndPoint e;
}