473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

UdpClient

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(Socke tOptionName
optionName,
MulticastOption MR)
at System.Net.Sockets.Socket.SetSocketOption(SocketOp tionLevel optionLevel,
ocketOptionName optionName, Object optionValue)
at System.Net.Sockets.UdpClient.JoinMulticastGroup(IP Address multicastAddr)
Nov 16 '05 #1
2 9507
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/...ti.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...er_7353_05.asp.

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

HTH

Dale Preston
MCAD, MCDBA, MCSE

"goodmannewz" <go*********@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
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(Socke tOptionName
optionName,
MulticastOption MR)
at System.Net.Sockets.Socket.SetSocketOption(SocketOp tionLevel optionLevel, ocketOptionName optionName, Object optionValue)
at System.Net.Sockets.UdpClient.JoinMulticastGroup(IP Address

multicastAddr)
Nov 16 '05 #2
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" <da******@nospam.nospam> wrote in message
news:eC*************@TK2MSFTNGP15.phx.gbl...
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/...ti.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...er_7353_05.asp.

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

HTH

Dale Preston
MCAD, MCDBA, MCSE

"goodmannewz" <go*********@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
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(Socke tOptionName
optionName,
MulticastOption MR)
at System.Net.Sockets.Socket.SetSocketOption(SocketOp tionLevel

optionLevel,
ocketOptionName optionName, Object optionValue)
at System.Net.Sockets.UdpClient.JoinMulticastGroup(IP Address

multicastAddr)

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephan Steiner | last post by:
Hi The project I'm currently working on involves sending large UDP broadcasts. As the .NET framework already provides an easy facility for sending and receiving UDP packets I thought it was a...
4
by: Morten Overgaard | last post by:
Hi I'm listening on the SysLog port (514) through UDP. The problem is that I am not receiving anything nut I know that i get messages on the port. When I use KIWI to listen on the same port via...
1
by: Dan Kelley | last post by:
I have 2 projects - 1 Winform project that sends Udp messages using the UdpClient class when a button is clicked, and a Console application that listens for these Udp messages. If I try to use...
2
by: Rick | last post by:
Hi, I'm trying to get a simple UdpClient app working. I've been looking at the MSDN info regarding UdpClient. When I set it up on my own PC and send messages to myself it works OK. If I try to...
2
by: Emilio | last post by:
Question about UdpClient sample ' This constructor arbitrarily assigns the local port number. Dim udpClient As New UdpClient() Try udpClient.Connect("www.contoso.com", 11000) ' Sends a...
3
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example,...
0
by: tccode97 | last post by:
To whom it may concern, I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link ...
1
by: tccode97 | last post by:
Hi, I need an urgent help. I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link ...
0
by: matt | last post by:
Hi I built a class to poll an app on a udp port using the sync methods which worked fine. I've since changed to using beginreceive and now the poll works but I don't receive the response from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.