Advertisement
Guest User

Test UDP Server

a guest
Aug 5th, 2016
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 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_Server
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             bool done = false;
  16.             UdpClient listener = new UdpClient(11000);
  17.             IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 11000);
  18.             string received_data;
  19.             byte[] receive_byte_array;
  20.             try
  21.  
  22.             {
  23.                 while (!done)
  24.                 {
  25.                     Console.WriteLine("Waiting for broadcast");
  26.                     // this is the line of code that receives the broadcase message.
  27.  
  28.                     // It calls the receive function from the object listener (class UdpClient)
  29.  
  30.                     // It passes to listener the end point groupEP.
  31.  
  32.                     // It puts the data from the broadcast message into the byte array
  33.  
  34.                     // named received_byte_array.
  35.  
  36.                     // I don't know why this uses the class UdpClient and IPEndPoint like this.
  37.  
  38.                     // Contrast this with the talker code. It does not pass by reference.
  39.  
  40.                     // Note that this is a synchronous or blocking call.
  41.  
  42.                     receive_byte_array = listener.Receive(ref groupEP);
  43.                     Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
  44.                     received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
  45.                     Console.WriteLine("data follows \n{0}\n\n", received_data);
  46.  
  47.                     listener.Send(receive_byte_array, 1024);
  48.                 }
  49.             }
  50.             catch (Exception e)
  51.             {
  52.                 Console.WriteLine(e.ToString());
  53.             }
  54.             listener.Close();
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement