Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

C# UDP broadcasting communication stopped to work

$
0
0

Hello all!

I build a networked application that discovers another components using UDP broadcast messages. It worked fine for a long time, but when I tried to run it again a few days ago it stopped to work only in my machine. The firewall and the antivirus are disabled, but the problem persists. These packets arrive to machine, confirmed through sniffer, but application does not receive nothing. I built a small application to isolate and it confirmed that the problem is in only my machine, all others work fine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace UDPTester
{
  class Program
  {
    private static string PARAMETER_SERVER = "server";
    private static string PARAMETER_CLIENT = "client";
    private static int PORT = 1555;

    static void Main(string[] args)
    {
      if (CheckParameter(args, PARAMETER_SERVER))
      {
        Server();
      }
      else if (CheckParameter(args, PARAMETER_CLIENT))
      {
        Client();
      }
      else
      {
        Console.WriteLine("Usage:");
        Console.WriteLine(" UDPTester.exe [-server|-client]");
      }
    }

    static bool CheckParameter(string[] args, string parameter)
    {
      foreach (string arg in args)
      {
        if (arg.ToLower().Equals("-"+parameter))
          return true;
        else if (arg.ToLower().Equals("/"+parameter))
          return true;
      }
      return false;
    }

    static void Server()
    {
      IPAddress[] LocalAddresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
      UdpClient UDPSocket = new UdpClient(new IPEndPoint(IPAddress.Any, PORT));
      UDPSocket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

      Console.WriteLine("Listening to port " + PORT);

      byte[] ReceivedData;
      IPEndPoint Source = null;
      while (true)
      {
        try
        {
          ReceivedData = UDPSocket.Receive(ref Source);

          Console.Write("*");
        }
        catch (Exception)
        {
          continue;
        }
      }
    }

    static void Client()
    {
      IPAddress[] LocalAddresses = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
      UdpClient UDPSocket = new UdpClient(new IPEndPoint(IPAddress.Any, PORT));
      UDPSocket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

      Console.WriteLine("Sending to port " + PORT);

      byte[] Message = new byte[2];
      Message[0] = (byte)1;
      Message[1] = (byte)0;

      IPEndPoint BroadcastEndPoint = new IPEndPoint(IPAddress.Broadcast, PORT);

      while (true)
      {
        UDPSocket.Send(Message, Message.Length, BroadcastEndPoint);
        Console.Write("+");

        Thread.Sleep(500);
      }
    }
  }
}

I dont know if any configuration or installed program changed this behaviour.

Someone could help me?

Thanks!


Viewing all articles
Browse latest Browse all 7132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>