怎样不停的侦听是否有UDP数据包,最好有代码参考一下

解决方案 »

  1.   

    IPHostEntry iphe = Dns.GetHostEntry("localhost");
            IPAddress ip = iphe.AddressList[0];
            Socket server = new Socket(ip.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(ip, 11000);
            server.Bind(ipep);        IPEndPoint ssender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint tmpSender = (EndPoint)ssender;
            while (true)
            {
                int recCount=0;
                Byte[] rec=new Byte[1024];
                int state=0;            recCount = server.ReceiveFrom(rec, 0, rec.Length, SocketFlags.None, ref tmpSender);
                string msg = UnicodeEncoding.ASCII.GetString(rec, 0, recCount);
             }
    大概意思是这样,就是循环监听,至于如何终止监听,处理缓冲区大小问题,楼主再看些相关文章吧。