MagnusArias

PS1 | UDP Client

May 15th, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace UDP_Client
  10. {
  11.     class Client
  12.     {
  13.         public static void Main()
  14.         {
  15.             string strIpAddress, menuOption;
  16.             IPAddress IpAddress = null;
  17.             Boolean tryAgain = true;
  18.             UdpClient udpclient = new UdpClient();
  19.             drawMenu();
  20.  
  21.             Console.WriteLine("What do you want to do?");
  22.             Console.WriteLine("\t1.\tMulticast\n\t2.\tBroadcast\n\tAny.\tExit");
  23.             Console.Write("Your choice: ");
  24.  
  25.             menuOption = Console.ReadLine();
  26.             if (menuOption.Equals("1"))
  27.             {
  28.                 while (tryAgain)
  29.                 {
  30.                     tryAgain = true;
  31.                     Console.Write("Enter multicast address: ");
  32.                     strIpAddress = Console.ReadLine();
  33.                     try
  34.                     {
  35.                         IpAddress = IPAddress.Parse(strIpAddress);
  36.                         if (!isAddressValid(IpAddress))
  37.                         {
  38.                             Console.WriteLine("Wrong multicast address, try again.");
  39.                             tryAgain = true;
  40.                         }
  41.                         else tryAgain = false;
  42.                     }
  43.                     catch (FormatException e)
  44.                     {
  45.                         Console.WriteLine("Wrong IP address, try again.");
  46.                         tryAgain = true;
  47.                     }
  48.                 }
  49.  
  50.                 udpclient.JoinMulticastGroup(IpAddress);
  51.             }
  52.             else if (menuOption.Equals("2"))
  53.             {
  54.                 strIpAddress = "255.255.255.255";
  55.                 IpAddress = IPAddress.Parse(strIpAddress);
  56.             }
  57.             else System.Environment.Exit(-1);
  58.  
  59.             IPEndPoint remoteep = new IPEndPoint(IpAddress, 2222);
  60.             Console.WriteLine("Send messages:");
  61.             string text = "";
  62.             while (true)
  63.             {
  64.                 Console.Write("Text:");
  65.                 text = Console.ReadLine();
  66.                 if (String.IsNullOrEmpty(text)) break;
  67.                 byte[] data = Encoding.Unicode.GetBytes(text);
  68.                 udpclient.Send(data, data.Length, remoteep);
  69.  
  70.                 Console.Write("rn");
  71.             }
  72.         }
  73.  
  74.         public static bool isAddressValid(IPAddress address)
  75.         {
  76.             IPAddress lowerAddress = IPAddress.Parse("224.0.0.0");
  77.             IPAddress higherAddress = IPAddress.Parse("239.255.255.255");
  78.             byte[] lowerBytes = lowerAddress.GetAddressBytes();
  79.             byte[] upperBytes = higherAddress.GetAddressBytes();
  80.             byte[] addressBytes = address.GetAddressBytes();
  81.  
  82.             bool lower = true, higher = true;
  83.  
  84.             for (int i = 0; i < lowerBytes.Length && (lower || higher); i++)
  85.             {
  86.                 if ((lower && addressBytes[i] < lowerBytes[i]) || (higher && addressBytes[i] > upperBytes[i]))
  87.                     return false;
  88.  
  89.                 lower &= (addressBytes[i] == lowerBytes[i]);
  90.                 higher &= (addressBytes[i] == upperBytes[i]);
  91.             }
  92.  
  93.             return true;
  94.         }
  95.  
  96.         public static void drawMenu()
  97.         {
  98.             Console.WriteLine("\n\t******************************");
  99.             Console.WriteLine(  "\t*          UDP Client        *");
  100.             Console.WriteLine(  "\t******************************\n");
  101.         }
  102.     }
  103. }
Add Comment
Please, Sign In to add comment