Simple C# UDP Nonblocking
Hi, Ive been looking all over for a simple UDP nonblocking C# example.
The few I find don't seem to work or aren't 2.0.
Any example would be great
Thank you!
using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;namespace udpasync { class Program { static UdpClient usck; static void recv(IAsyncResult res) { IPEndPoint remote = new IPEndPoint(IPAddress.Any,0); byte[] data = usck.EndReceive(res, ref remote); // do something with data received from remote Console.WriteLine(remote.Address.ToString() + ": " + Encoding.ASCII.GetString(data)); // get next packet usck.BeginReceive(recv, null); } static void Main(string[] args) { usck = new UdpClient(12000); Console.WriteLine("waiting for packets, hit enter to stop"); usck.BeginReceive(new AsyncCallback(recv), null); Console.ReadLine(); usck.Close(); } }}
that's one way to sking the cat... I would do it with blocking, though, but inside a different thread (so the main thread doesn't block), but I guess it depends o what you want to use it for.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement