// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Net; using System.Net.Sockets; namespace UDP.Client { public class Program { private const string SERVER = "localhost"; private const int SERVER_PORT = 12345; private const int CLIENT_PORT = 12346; private const int SIZE = 7; private const int MAX_SIZE = 1000; public static void Main(string[] args) { using(UdpClient cli = new UdpClient(CLIENT_PORT)) { for(int i = 0; i < 3; i++) { // setup data byte[] buf1 = new byte[SIZE]; for(int j = 0; j < buf1.Length; j++) buf1[j] = (byte)(i + j); // send packet cli.Send(buf1, buf1.Length, SERVER, SERVER_PORT); // receive packet IPEndPoint from = new IPEndPoint(IPAddress.Any, 0); byte[] buf2 = cli.Receive(ref from); // print data for(int j = 0; j < buf2.Length; j++) Console.Write(" " + buf2[j]); Console.WriteLine(); } } } } }