Advertisement
MagnusArias

PS1 | UDP Server

May 15th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 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
  10. {
  11.     class Server
  12.     {
  13.         public static void Main()
  14.         {
  15.             string strIpAddress;
  16.             IPAddress IpAddress = null;
  17.             Boolean tryAgain = true;
  18.             drawMenu();
  19.  
  20.             while (tryAgain)
  21.             {
  22.                 tryAgain = true;
  23.                 Console.Write("Enter multicast address: ");
  24.                 strIpAddress = Console.ReadLine();
  25.                 try
  26.                 {
  27.                     IpAddress = IPAddress.Parse(strIpAddress);
  28.                     if (!IsInRange(IpAddress))
  29.                     {
  30.                         Console.WriteLine("Wrong multicast address, try again.");
  31.                         tryAgain = true;
  32.                     }
  33.                     else tryAgain = false;
  34.                 }
  35.                 catch (FormatException e)
  36.                 {
  37.                     Console.WriteLine("Wrong IP address, try again.");
  38.                     tryAgain = true;
  39.                 }
  40.             }
  41.  
  42.             UdpClient client = new UdpClient();
  43.             client.ExclusiveAddressUse = false;
  44.             IPEndPoint localEp = new IPEndPoint(IPAddress.Any, 2222);
  45.             client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  46.             client.ExclusiveAddressUse = false;
  47.             client.Client.Bind(localEp);
  48.             client.JoinMulticastGroup(IpAddress);
  49.  
  50.             Console.WriteLine("Server started...\n\n");
  51.  
  52.             while (true)
  53.             {
  54.                 Byte[] data = client.Receive(ref localEp);
  55.                 string strData = Encoding.Unicode.GetString(data);
  56.                 Console.WriteLine(strData);
  57.             }
  58.         }
  59.  
  60.         public static bool IsInRange(IPAddress address)
  61.         {
  62.             IPAddress lowerAddress = IPAddress.Parse("224.0.0.0");
  63.             IPAddress higherAddress = IPAddress.Parse("239.255.255.255");
  64.             byte[] lowerBytes = lowerAddress.GetAddressBytes();
  65.             byte[] upperBytes = higherAddress.GetAddressBytes();
  66.             byte[] addressBytes = address.GetAddressBytes();
  67.  
  68.             bool lower = true, higher = true;
  69.  
  70.             for (int i = 0; i < lowerBytes.Length && (lower || higher); i++)
  71.             {
  72.                 if ((lower && addressBytes[i] < lowerBytes[i]) || (higher && addressBytes[i] > upperBytes[i]))
  73.                     return false;
  74.  
  75.                 lower &= (addressBytes[i] == lowerBytes[i]);
  76.                 higher &= (addressBytes[i] == upperBytes[i]);
  77.             }
  78.  
  79.             return true;
  80.         }
  81.  
  82.         public static void drawMenu()
  83.         {
  84.             Console.WriteLine("\n\t******************************");
  85.             Console.WriteLine(  "\t*          UDP Server        *");
  86.             Console.WriteLine(  "\t******************************\n");
  87.         }
  88.  
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement