title: C# UDP listener not accepting packets untill another program sends something
body: <p>Right, hopefully a quick question &amp; answer.
  My application is supposed to listen on an UDP port and from any IP Address from a certain showcontrol server. Which just sends out plain ASCII text/strings.</p>
  
  <p>Since we do not have such a S.C. server at the office I also wrote a very small simple console application that just broadcasts UDP packets.</p>
  
  <p>This is the console app:</p>
  
  <pre><code>    static void Main(string[] args)
      {
  
          Thread.Sleep(1000);
  
          Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          IPEndPoint e = new IPEndPoint(IPAddress.Broadcast, 35000);
  
          IPAddress broadcast = IPAddress.Parse("192.168.1.255");
  
          e = new IPEndPoint(broadcast, 35000);
  
          string s;
          Byte[] sendBytes;
  
          while ("exit" != (s = Console.ReadLine()))
          {
              sendBytes = Encoding.ASCII.GetBytes((s.EndsWith("\n") ? s : s + "\n"));
              soc.SendTo(sendBytes, e);
          }
  
          sendBytes = Encoding.ASCII.GetBytes("EXIT\n");
          soc.SendTo(sendBytes, e);
  
          soc.Close();
      }
  </code></pre>
  
  <p>I've tried two things on the listener part. Using UdpClient and using a socket connection and both are giving me the exact same problem which I will describe below both my listener code:</p>
  
  <p>The Socket version:</p>
  
  <pre><code>    public MediaPlayer(Output outputWindow)
      {
          Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  
          IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, i_inPort);
  
          serverSocket.Bind(ipEndPoint);
  
          IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
  
          EndPoint epSender = (EndPoint)ipeSender;
  
          SocketState state = new SocketState();
          state.e = epSender;
          state.s = serverSocket;
  
          beginReceiveFrom(state);
      }
      public void beginReceiveFrom(SocketState state)
      {
          state.byteData = new byte[state.byteData.Length];
          IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
          state.e = (EndPoint)ipeSender;
  
          state.s.BeginReceiveFrom(state.byteData, 0, state.byteData.Length, SocketFlags.None, ref state.e, onReceiveFrom, state);
      }
  
      public void onReceiveFrom(IAsyncResult ar)
      {
          SocketState state = (SocketState)ar.AsyncState;
          Socket serverSocket = state.s;
          //EndPoint epSender = state.e;
  
          IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
          EndPoint epSender = (EndPoint)ipeSender;
          try
          {
              serverSocket.EndReceiveFrom(ar, ref epSender);
  
              string s = Encoding.ASCII.GetString(state.byteData);
          }
          catch (System.Exception ex)
          {
              Console.WriteLine(ex);
          }
          finally
          {
              beginReceiveFrom(state);
          }
      }
  </code></pre>
  
  <p>The UdpClient version:</p>
  
  <pre><code>    public MediaPlayer(Output outputWindow)
      {
          UdpClient client = new UdpClient(i_inPort);
          IPEndPoint endPoint = null;
  
          UdpState state = new UdpState();
  
          state.e = endPoint;
          state.u = client;
      }
  
      private void BeginReceive(UdpState state)
      {
          state.u.BeginReceive(new AsyncCallback(ReceiveCallback), state);
      }
  
      private void ReceiveCallback(IAsyncResult ar)
      {
          UdpState state = null;
  
          try
          {
              state = (UdpState)ar.AsyncState;
  
              Byte[] receiveBytes = state.u.EndReceive(ar, ref state.e);
              string receiveString = Encoding.ASCII.GetString(receiveBytes);
  
              executive.Receive(receiveString);
  
              BeginReceive(state);
          }
          catch(System.Exception ex)
          {
              SetText(ex.ToString());
          }       
      }
  </code></pre>
  
  <p>There is some extra code in the UpdClient ReceiveCallback version because that is the one I initially used.
  The problem is, is that when I am sending/broadcasting stuff with the console app within anyway in my companies network and on any computer it just works, no problem. Multiple mediaplayers can be playing on any amount of computers with no problem.</p>
  
  <p>However, when I am using a different UDP sender (Something like Hercules from hw-group.com) then it simply wont work, >untill&lt; I send one command with my console app. Only then, after sending a text/packet/data with my console app will my applcation accept data from the hercules application.</p>
  
  <p>Is there anyone seeing what I am doing wrong? I've read multiple code examples, from here, codeproject and guru-something, and they are all using the same type of programming that I used.</p>
  
  <p>Also, sorry for the wall of text.</p>