G
Guest
I just cannot run the sample code from msdn. Please tell me why? Thanks a lot.
I create a console C# project, parsed the example codes:
public class UDPMulticastListener
{
private static readonly IPAddress GroupAddress =
IPAddress.Parse("192.168.0.1");
private const int GroupPort = 9200;
private static void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
try
{
listener.JoinMulticastGroup(GroupAddress);
listener.Connect(groupEP);
while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}
listener.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
[STAThread]
public static int Main(String[] args)
{
StartListener();
Console.ReadLine();
return 0;
}
}
The following runtime error:
System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.Sockets.Socket.setMulticastOption(SocketOptionName
optionName,
MulticastOption MR)
at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel,
ocketOptionName optionName, Object optionValue)
at System.Net.Sockets.UdpClient.JoinMulticastGroup(IPAddress multicastAddr)
I create a console C# project, parsed the example codes:
public class UDPMulticastListener
{
private static readonly IPAddress GroupAddress =
IPAddress.Parse("192.168.0.1");
private const int GroupPort = 9200;
private static void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient();
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
try
{
listener.JoinMulticastGroup(GroupAddress);
listener.Connect(groupEP);
while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}
listener.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
[STAThread]
public static int Main(String[] args)
{
StartListener();
Console.ReadLine();
return 0;
}
}
The following runtime error:
System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.Sockets.Socket.setMulticastOption(SocketOptionName
optionName,
MulticastOption MR)
at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel,
ocketOptionName optionName, Object optionValue)
at System.Net.Sockets.UdpClient.JoinMulticastGroup(IPAddress multicastAddr)