May I suggest a snippet from my own code that I use all the time?
You'll need to adjust a couple variable names to match your code, but I use names that are easy to read.
listenPort is set before calling this method, rather than hard code values into it.
I commented out the line to convert the received data to ASCII since I don't know if that applies to what you are doing.
Something to note that might help: IPAddress.Any
This way you aren't listening for messages from a specific address, but from any address. Once you know that works, you can narrow down just one address.
-
public void StartListener()
-
{
-
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
-
done = false;
-
try
-
{
-
while (!done)
-
{
-
Console.WriteLine("Waiting for broadcast");
-
if (listener != null)
-
{
-
byte[] bytes = listener.Receive(ref groupEP);
-
string FullMsg = Encoding.Default.GetString(bytes, 0, bytes.Length);
-
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
-
groupEP.ToString(), FullMsg);
-
/*Encoding.ASCII.GetString(bytes, 0, bytes.Length));*/
-
ReceivedMsg(FullMsg);// Raise and event with the message, for any class that subscribes
-
}
-
}
-
}
-
catch (Exception e)
-
{
-
Console.WriteLine(e.ToString());
-
}
-
finally
-
{
-
if (listener !=null) listener.Close();
-
StartListener();
-
}
-
}
-
public void StopListener()
-
{
-
done = true;
-
}
-
-