UdpClient receive on broadcast address - c#

In c# I am using the UdpClient.Receive function:
public void StartUdpListener(Object state)
{
try
{
udpServer = new UdpClient(new IPEndPoint(IPAddress.Broadcast, 1234));
}
catch (SocketException ex)
{
MessageBox.Show(ex.ErrorCode.ToString());
}
IPEndPoint remoteEndPoint = null;
receivedNotification=udpServer.Receive(ref remoteEndPoint);
...
However I am getting a socket exception saying that the address is not available with error code 10049
What do I do to negate this exception?

Here's the jist of some code I am currently using in a production app that works (we've got a bit extra in there to handle the case where the client are server apps are running on a standalone installation). It's job is to receive udp notifications that messages are ready for processing. As mentioned by Adam Alexander your only problem is that you need to use IPAddress.Any, instead of IPAddress.Broadcast. You would only use IPAddress.Broadcast when you wanted to Send a broadcast UDP packet.
Set up the udp client
this.broadcastAddress = new IPEndPoint(IPAddress.Any, 1234);
this.udpClient = new UdpClient();
this.udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
this.udpClient.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.
And to trigger the start of an async receive using a callback.
this.udpClient.Client.Bind(this.broadcastAddress);
this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
Hopefully this helps, you should be able to adapt it to working synchronously without too much issue. Very similar to what you are doing. If you're still getting the error after this then something else must be using the port that you are trying to listen on.
So, to clarify.
IPAddress.Any = Used to receive. I want to listen for a packet arriving on any IP Address.
IPAddress.Broadcast = Used to send. I want to send a packet to anyone who is listening.

for your purposes I believe you will want to use IPAddress.Any instead of IPAddress.Broadcast. Hope this helps!

That error means the protocol cant bind to the selected IP/port combination.
I havent used UDP broadcast in ages, but I do recall you need to use different IP ranges.

There's nothing wrong with the way you have configured your UdpClient. Have you tried a different port number? Perhaps 1234 is already in use on your system by a different app.

Related

UDP client assigns free port too late

In C#, .Net 4, I have a need to send a UDP message on a port and listen for responses on the same port.
I was using a fixed port, but a customer ran into an issue with that, so I want to use any available port. It seems like I can do that by using 0 for the port number, but it isn't working. Digging into that some more, it appears it doesn't assign the port until it is actually used, so my two bind statements might be going to different ports.
From MSDN:
"If you do not care which local port is used, you can create an
IPEndPoint using 0 for the port number. In this case, the service
provider will assign an available port number between 1024 and 5000.
If you use the above approach, you can discover what local network
address and port number has been assigned by calling the
LocalEndPoint. ... If you are using a connectionless protocol, you
will not have access to this information until you have completed a
send or receive."
Trouble is, I want to set up my send and receive on initialization. If I wait until the first send to set up the receive, I might miss some responses. Is there a better answer than just sending a garbage message to assign the port so I can finish the initialization?
My code:
public bool InitializeSockets()
{
try
{
IPAddress localaddr = LocalIPAddress();
localep = new IPEndPoint(localaddr, 0); //(was port 50000);
//-----------------------------------------------------------------
// set up listener port for responses coming back on the same port
//-----------------------------------------------------------------
listener = new UdpClient();
listener.ExclusiveAddressUse = false;
listener.Client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
listener.Client.Bind(localep);
detailsOutputText = "Ready to listen on " + localep;
ustate = new UdpState();
ustate.e = localep;
ustate.u = listener;
//------------------------
// set up broadcast port
//------------------------
bcast = new UdpClient();
bcast.Client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
bcast.Client.Bind(localep);
//-------------------------------
// start listening for responses
//-------------------------------
msgRxCallback = listener.BeginReceive(new AsyncCallback(DiscoveryCallback), ustate);
return true;
}
catch (Exception exc)
{
if (exc is SocketException)
{
// This only catches if another process has opened that port without sharing it
// or if firewall blocks it?
MessageBox.Show("Error opening IP address:Port : " + localep;
}
else
MessageBox.Show(exc.ToString());
return false;
}
}
Thanks
Two options:
Create a configuration file that contains the port number. On initialization, read the config file and use that port number when setting up the clients. Supply a config file with a default port number, and give your customer instructions about how to change it if required.
In your initialization, create a receiver, give it a very short receive timeout, and call Receive. That will cause the port to bind. You can then get the local end point and use that when you create your sender. See Can I set the timeout for UdpClient in C#? regarding setting the receive timeout.

C# application simply not receiving UDP data

I am trying to do something very simple that does not work: With my C# application I simply want to listen for UDP packages on a specific port. With Wireshark I can see very well that the packages that I desire are received perfectly fine (CRC and everything ok).
However, none of the codes I found on internet work. For example this one failes as well:
private void ReceiveAsync()
{
UdpClient Client = new UdpClient(new IPEndPoint(IPAddress.Any, 51200));
try
{
Client.BeginReceive(new AsyncCallback(OnReceive), null);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
private static void OnReceive(IAsyncResult result)
{
System.Windows.Forms.MessageBox.Show("Simply to tell you that something was received on the port");
}
There are two ethernet network cards installed. Maybe this can be a problem? But even if I specify the IP address specifically it would not change anything.
UdpClient Client = new UdpClient(new IPEndPoint(IPAddress.Parse("10.0.0.2"), 51200));
I would be very happy about any ideas that could solve this problem. Thank you very much!
The code is fine and working - I have tested it.
You need to wait for received data, Client object exist only in ReceiveAsync
Try adding Thread.Sleep(10000)
Edit:
Thread.Sleep(1000) is not good practice since it block the thread.
it's depend on the problem/case that you are trying to solve. you may have some kind of TCP engine that handle multiple connection ,or data processing so you can say data on buffer for some other thread to work on.
If you share the problem that you are trying to solve , maybe can give better answer
Also can see the code example from MSDN - UdpClient.BeginReceive
I found this thread in here
Receive messages continuously using udpClient
May be this helps.
you should really not show a message box, think about a debug output.
save your input directly after receiving, to avoid blocking the network.

2 instances of the same program broadcasting via UDPClient

I'm using UdpClient to broadcast my program location so a centralize computer can find it and start working with it.
It works great, until 2 users on the same computer at the same time try to broadcast. Since this is a valid use case (think Windows Terminal Server), I'm trying to figure out the best way to address it:
Here is the code I'm currently trying:
m_UDPClients = new List<UdpClient>();
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in localIPs)
{
string ipStr = ip.ToString();
if (!ipStr.Contains(".")) // I only want readable addresses
continue;
IPEndPoint ipLocalEndPoint = new IPEndPoint(ip, port);
try
{
UdpClient udpClient = new UdpClient();
udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpClient.Client.Bind(ipLocalEndPoint);
m_UDPClients.Add(udpClient);
}
catch (Exception e)
{
BSSLog.Error(e);
}
}
if there is another instance of the program running already, the bind will throw this exception:
An attempt was made to access a socket in a way forbidden by its access permissions
This broadcast is just the program 'advertising' some details about how to connect to it's services; The broadcast happens every few seconds and it's payload is probably < 1k.
I did try to create and destroy the UDPclients as needed and just accept the collision, but it was ugly.
So...the question is: How do I handle 30 instances of the same program all attempting to advertise themselves via the same UDP port (no listening, just post the payload and move on) on a single computer?
I know I could build a Windows service and play traffic cop, etc... but I'm trying to keep this simple and 'limited user rights' friendly.
Before I run off and attempt to use a predefined system wide named pipe or something, I thought I was ask for help. :)
There isn't anything wrong with the code. In our test environment we had a monitoring program that wasn't opening the port correctly.
Good thing we wasted 2 days chasing a red herring.
thank you everyone.

TcpClient connect fails with IPv6Any

The problem is, the following code works well if IPAddress.Any was given as a parameter, but throws an error if `IPAddress.IPv6Any is used.
I receive error #10057
Socket is not connected.
A request to send or receive data was disallowed because the socket is
not connected and (when sending on a datagram socket using
sendto) no address was supplied. Any other type of operation might
also return this error—for example, setsockopt setting SO_KEEPALIVE if
the connection has been reset.
Why does it fails to work as IPv6Any? I'm pretty sure it's not the firewall, since the port remains the same and it works with IPv4 (and my firewall should pass any requests made by my application).
To short up the code, it's something like this:
The Listener:
listener = new TcpListener(IPAddress.IPv6Any, portNr);
listener.AllowNatTraversal(true);
listener.Start();
listener.BeginAcceptTcpClient(this.AcceptClient, null);
The Client:
client = new TcpClient();
client.NoDelay = true;
try
{
this.client.Connect(ip, port); //ip = "localhost" when connecting as server
}
catch (Exception ex)
{
FileLogger.LogMessage(ex);
Disconnect();
}
I'm trying to set up the "server-side" of the TCP-connection.
What I do is that I start a listener at localhost, and then connect to it as a client (and allow others to join as clients as well).
What I'm trying to achieve with this is direct addressability of this TCP server, following this article: http://blogs.msdn.com/b/ncl/archive/2009/07/27/end-to-end-connectivity-with-nat-traversal-.aspx
The reason I'm doing this is that I want person A to be able to connect to a person B when they both are behind NAT routers.
I know this answer comes a bit late, but I also had this issue and it was client related. The problem is, that your provided code...
client = new TcpClient();
... creates an IPv4-Instance of the TcpClient that is not capable of interpreting IPv6-Addresses. So if you already have the IP address at the moment of initialization, try to initialize your TcpClient like this:
TcpClient client = new TcpClient(ip.AddressFamily);
If the variable ip is a string, you need to convert it to type IPAddress first:
IPAddress iAddr = IPAddress.Parse(ip);
Actually a IPv6-TcpClient seems to be compatible to IPv4-Addresses as well, so you can also initialize your client as follows:
TcpClient client = new TcpClient(AddressFamily.InterNetworkV6)
Whilst the upper suggestions seem to be the cleanest ones, the bottom suggestion seems to be the more universal one. At the end it's up to your preferences.
I solved a similar issue where the following line would only block connections coming from IPv4 addresses:
listener = new TcpListener(IPAddress.IPv6Any, portNr);
It seems the socket when configured to accept IPv6 connections, by default, accepts ONLY IPv6 connections. To fix this problem i had to update my code to this:
listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
listener.Start();
When you use TcpClient.Connect(string, int) you make it possible to break because of DNS resolution.
Though Microsoft documents that IPv6 address will be tried first, the resolution may only return IPv4 addresses.
http://msdn.microsoft.com/en-us/library/8s2yca91.aspx
Therefore, can you try to call TcpClient.Connect(IPAddress.IPv6Loopback, port) on client side to test if it works?
IPAddress.Loopback == FAIL
IPAddress.IPv6Loopback == SUCCESS
Perhaps localhost is mapping to the IPv4 Loopback Address in your case?

How to do a UDP multicast across the local network in c#?

I am trying to get some simple UDP communication working on my local network.
All i want to do is do a multicast to all machines on the network
Here is my sending code
public void SendMessage(string message)
{
var data = Encoding.Default.GetBytes(message);
using (var udpClient = new UdpClient(AddressFamily.InterNetwork))
{
var address = IPAddress.Parse("224.100.0.1");
var ipEndPoint = new IPEndPoint(address, 8088);
udpClient.JoinMulticastGroup(address);
udpClient.Send(data, data.Length, ipEndPoint);
udpClient.Close();
}
}
and here is my receiving code
public void Start()
{
udpClient = new UdpClient(8088);
udpClient.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);
receiveThread = new Thread(Receive);
receiveThread.Start();
}
public void Receive()
{
while (true)
{
var ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
var data = udpClient.Receive(ref ipEndPoint);
Message = Encoding.Default.GetString(data);
// Raise the AfterReceive event
if (AfterReceive != null)
{
AfterReceive(this, new EventArgs());
}
}
}
It works perfectly on my local machine but not across the network.
-Does not seem to be the firewall. I disabled it on both machines and it still did not work.
-It works if i do a direct send to the hard coded IP address of the client machine (ie not multicast).
Any help would be appreciated.
Does your local network hardware support IGMP?
It's possible that your switch is multicast aware, but if IGMP is disabled it won't notice if any attached hardware subscribes to a particular multicast group so it wouldn't forward those packets.
To test this, temporarily connect two machines directly together with a cross-over cable. That should (AFAICR) always work.
Also, it should be the server half of the code that has the TTL argument supplied to JoinMulticastGroup(), not the client half.
I've just spent 4 hours on something similar (I think), the solution for me was:
client.Client.Bind(new IPEndPoint(IPAddress.Any, SSDP_PORT));
client.JoinMulticastGroup(SSDP_IP,IP.ExternalIPAddresses.First());
client.MulticastLoopback = true;
Using a specific (first external) IP address on the multicast group.
I can't see a TTL specified anywhere in the code. Remember that TTL was originally meant to be in unit seconds, but is has become unit hops. This means that by using a clever TTL you could eliminate passing through the router. The default TTL on my machine is 32 - I think that should be more than adequate; but yours may actually be different (UdpClient.Ttl) if your system has been through any form of a security lockdown.
I can't recommend the TTL you need - as I personally need to do a lot of experimentation.
If that doesn't work, you could have a look at these articles:
OSIX Article
CodeProject Article
All-in-all it looks like there has been success with using Sockets and not UdpClients.
Your chosen multicast group could also be local-only. Try another one.
Your physical network layer could also be causing issues. I would venture to question switches and direct (x-over) connections. Hubs and all more intelligent should handle them fine. I don't have any literature to back that, however.

Categories

Resources