Simple C# UDP Nonblocking

Started by
1 comment, last by MetaKnight 17 years, 2 months ago
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!
Advertisement
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.
Q: How many programmers does it take to write a nice piece of software?A: MORE.
Ah perfect! Thank you
I really wish the documentation was better on this on MSDN

This topic is closed to new replies.

Advertisement