UdpClient

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)
 
D

Dale Preston

First, 192.168 is not a multicast IPAddress. Use the 224 address that the
original sample shows or a valid multicast address. For a relatively simple
explanation, go to:

http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/ipmulti.htm#xtocid4

If you want the details (this link is for trained professionals, do not
attempt to read it at home):

http://www.iana.org/assignments/multicast-addresses

Then, add the IP port to your UdpClient constructor:

UdpClientListener = new UdpClientListener(GroupPort)

This seems to be either an unducommented feature of the framework, that the
port has to be specified to use JoinMulticastGroup:

Finally, and the easy route, here's a great example I found onlne:

http://www.vbip.com/books/1861007353/chapter_7353_05.asp.

I liked the example enough that I just ordered the book.

HTH

Dale Preston
MCAD, MCDBA, MCSE

goodmannewz said:
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)
 
D

Dale Preston

Sorry, there's a typo in my sample. Can I offer as an excuse that I'm
temporarily typing on my laptop?

The UdpClient constructor should have read:

UdpClient listener = new UdpClient(GroupPort);

Dale Preston

Dale Preston said:
First, 192.168 is not a multicast IPAddress. Use the 224 address that the
original sample shows or a valid multicast address. For a relatively simple
explanation, go to:

http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/ipmulti.htm#xtocid4

If you want the details (this link is for trained professionals, do not
attempt to read it at home):

http://www.iana.org/assignments/multicast-addresses

Then, add the IP port to your UdpClient constructor:

UdpClientListener = new UdpClientListener(GroupPort)

This seems to be either an unducommented feature of the framework, that the
port has to be specified to use JoinMulticastGroup:

Finally, and the easy route, here's a great example I found onlne:

http://www.vbip.com/books/1861007353/chapter_7353_05.asp.

I liked the example enough that I just ordered the book.

HTH

Dale Preston
MCAD, MCDBA, MCSE

goodmannewz said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top