Multicast Invalid Argument

N

Nuno Magalhaes

I'm trying to set a multicast server but I get "Invalid argument" when
setting multicast options.
Below is the code that gives me problems more precisely in
SetSocketOption with the AddMembership name... it exits with a runtime
error telling me "An invalid argument was supplied". Does anyone know
how to fix this?
-------------------------------
public MulticastServer(string IPAddressString)
{
//Create multicast socket
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPAddress ip=IPAddress.Parse(IPAddressString);
socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,new
MulticastOption(ip));
socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive,2);
IPEndPoint ep=new IPEndPoint(ip,5000);
//socket.Connect(ep);
}
 
J

Jon Skeet [C# MVP]

Nuno said:
I'm trying to set a multicast server but I get "Invalid argument" when
setting multicast options.
Below is the code that gives me problems more precisely in
SetSocketOption with the AddMembership name... it exits with a runtime
error telling me "An invalid argument was supplied". Does anyone know
how to fix this?
-------------------------------
public MulticastServer(string IPAddressString)
{
//Create multicast socket
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPAddress ip=IPAddress.Parse(IPAddressString);
socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,new
MulticastOption(ip));
socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive,2);
IPEndPoint ep=new IPEndPoint(ip,5000);
//socket.Connect(ep);
}

The docs say:
<quote>
Windows 98, Windows NT 4.0 Platform Note: You must call the Bind
method before using AddMembership as the optionName parameter.
</quote>

Have you tried that?

Jon
 
N

Nuno Magalhaes

I didn't try but now it's another on bind: "The requested address isn't
valid". The IPAddressString is 224.1.2.3 and the code is below:
---------------------------------
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPAddress ip=IPAddress.Parse(IPAddressString);
IPEndPoint ep=new IPEndPoint(ip,5000);
socket.Bind(ep);
 
J

Jon Skeet [C# MVP]

Nuno said:
I didn't try but now it's another on bind: "The requested address isn't
valid". The IPAddressString is 224.1.2.3 and the code is below:
---------------------------------
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPAddress ip=IPAddress.Parse(IPAddressString);
IPEndPoint ep=new IPEndPoint(ip,5000);
socket.Bind(ep);

And is the machine you're running on 224.1.2.3?

Jon
 
N

Nuno Magalhaes

No but I fixed it now.
In Ethereal the socket send command will generate entries like this:
Time: 0.0s
Source: 10.154.0.104 SourcePort:5000
Destination: 224.1.2.3 DestPort:5000
Is this normal since my code is written like shown below. Is this a
good programming style?
Thanks for the comments.
-------------------------------
public MulticastServer(string IPAddressString)
{
//Create multicast socket
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPHostEntry IPHost=Dns.GetHostByName(Dns.GetHostName());
IPAddress ip=IPAddress.Parse(IPHost.AddressList[0].ToString());
IPEndPoint ep=new IPEndPoint(ip,5000);
socket.Bind(ep);

IPAddress ip2=IPAddress.Parse(IPAddressString);
IPEndPoint ep2=new IPEndPoint(ip2,5000);
socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,new
MulticastOption(ip2));
socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive,2);
socket.Connect(ep2);
}
 
J

Jon Skeet [C# MVP]

Nuno Magalhaes said:
No but I fixed it now.
In Ethereal the socket send command will generate entries like this:
Time: 0.0s
Source: 10.154.0.104 SourcePort:5000
Destination: 224.1.2.3 DestPort:5000
Is this normal since my code is written like shown below. Is this a
good programming style?

I'm not sure why it wouldn't be normal - what exactly are you asking
about? The code is fairly reasonable, although I suspect there are
better ways of getting the local endpoint. (I don't do this often
enough to know the best way offhand, I'm afraid.)
 

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

Top