Pred kompiláciou zdrojového kódu treba zmeniť IP adresu v oboch programoch.V kóde je IP servera nastavená ako localhost 127.0.0.1 a port 10123. Po spustení servera začne server načúvať na nastavenom porte. Po spustení klienta sa klient pripojí a odošle serveru našu správu.Server po obdržaní správy ju zobrazí a pošle potvrdenie, ktoré dostane klient. Klient môže byt spustený na rovnakom pocítaci ako server alebo z iného počítača.
Rozdiel v kóde oproti TCP:
TCP:
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
UDP:
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
UDP client v c#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; namespace UDP_Client_socket { class Program { static void Main(string[] args) { try { String str = String.Empty; IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10123); byte[] buffer = new byte[4096]; //buffer 4kB Console.WriteLine("Connecting....."); while (true) { //zadame text na odoslanie Console.WriteLine("Enter the string to be transmitted : "); //nacitanie zadaneho textu do premennej str = Console.ReadLine(); //Ak zadam slovo koniec,tak sa ukonci if (str == "koniec") break; using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { //Convertovenie stringu na byte array byte[] data = Encoding.ASCII.GetBytes(str); //odoslanie spravy sock.SendTo(data, data.Length, SocketFlags.None, iep); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)sender; //prijatie odpovede od servera int numberOfBytes = sock.ReceiveFrom(buffer, ref Remote); byte[] formatedBytes = new byte[numberOfBytes]; Array.Copy(buffer, formatedBytes, numberOfBytes); //Vypiseme co poslal server Console.WriteLine("Server reply : "); Console.WriteLine(ASCIIEncoding.ASCII.GetString(formatedBytes.ToArray())); // Console.ReadLine(); } } } catch (Exception e) { Console.WriteLine("Error..... " + e.Message); Console.ReadLine(); } } } }
UDP server v c#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; namespace UDP_Server_socket { class Program { static void Main(string[] args) { byte[] buffer = new byte[4096];//4kb buffer try { Console.WriteLine("Server is running"); Console.WriteLine("Waiting for connections..."); using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { //nacuvanie od vsetkych IP adries IPEndPoint iep = new IPEndPoint(IPAddress.Any, 10123); sock.Bind(iep); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); while (true) { int numberOfBytes = sock.ReceiveFrom(buffer, ref Remote); byte[] formatedBytes = new byte[numberOfBytes]; Array.Copy(buffer, formatedBytes, numberOfBytes); //Vypiseme co poslal server Console.WriteLine("Client send : "); Console.WriteLine(ASCIIEncoding.ASCII.GetString(formatedBytes)); //Odpoved servera byte[] data = Encoding.ASCII.GetBytes("dakujem za spravicku"); sock.SendTo(data, data.Length, SocketFlags.None, Remote); } } } catch (Exception ex1) { Console.WriteLine(ex1.Message); } } } }