1

I'm using UdpClient to send data on multicast address.

The code looks like this:

m_udpclientSender = new UdpClient();
m_remoteEndPoint = new IPEndPoint(m_multicastAddress, m_port);
m_udpclientSender.ExclusiveAddressUse = false;
m_udpclientSender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
m_udpclientSender.JoinMulticastGroup(m_multicastAddress, 255);
m_udpclientSender.ExclusiveAddressUse = false;
m_udpclientSender.Send(buffer, buffer.Length, m_remoteEndPoint);

It is sent to the correct port/ip, but it issued from a random port(which is expected), but I need/want this to be sent from a specific port(the same port that I'm sending too).

I saw that: How to specify source port of a UdpPacket?

But I need to NOT exclusively use address, and if I give this in the constructor, I got an exception(saying that this is already bound).

I've to put the same port because the protocol defines that the response should not be multicasted.

Community
  • 1
  • 1
J4N
  • 19,480
  • 39
  • 187
  • 340
  • The counterpart of your udpclient listens already on the specified port, right? so it knows to which port it has to answer (same port it was listening on). So why do you want to specify the source port? – faceman Feb 19 '13 at 10:12
  • mDNS protocol defines that IF the sender port is NOT 5353(mDNS port), it's an UNICAST query. Meaning that I will not receive it in the multicast group that I'm listening(and that other will not receive this answer too). – J4N Feb 19 '13 at 11:24
  • I Agree with David Pfeffer, but in your code you'll still have a problem with the ExclusiveAddressUse Property. Check this out: [link](http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.exclusiveaddressuse.aspx). Probably it would be better for you to directly use the socket class. – faceman Feb 19 '13 at 13:00

2 Answers2

1

You've done everything correct, but it sounds like the other user of the port already has exclusive port access. WinSock will not let you send from this port.

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
0

In fact, according to the documentation:

This property must be set before the underlying socket is bound to a client port. If you call UdpClient.UdpClient(Int32), UdpClient.UdpClient(Int32, AddressFamily), UdpClient.UdpClient(IPEndPoint), or UdpClient.UdpClient(String, Int32), the client port is bound as a side effect of the constructor, and you cannot subsequently set the ExclusiveAddressUse property

So it's impossible to set the ExclusiveAddressUse to false when setting the source port. I used the Socket directly

J4N
  • 19,480
  • 39
  • 187
  • 340