.NET Framework - Trouble binding a UDPClient

Asked By Davi
07-Feb-08 06:50 PM
I’m trying to use a UDP socket, but it isn’t working.

Here’s stripped down code of what I’m trying to do:



public void BindASocket()
{

System.Net.Sockets.UdpClient udpreceiver;


System.Net.IPEndPoint localreceiver;
System.Net.IPAddress localip;



localip = System.Net.IPAddress.Parse(tbLocalIP.Text);
localreceiver = new System.Net.IPEndPoint(localip, 0);

udpreceiver = new System.Net.Sockets.UdpClient();
udpreceiver.Client.Bind(localreceiver);

}

What I want to do is set up a receiver, and then tell it to receive whatever
comes its way.  I create the receiver, and I want to bind it to an address,
which I type into a text box on a form.  When I run the above, no matter what
IP address I put into the box, I get an excpetion with the message:

The requested address is not valid in its context

I’ve tried the local address of the PC, something that isn’t the local
address, “any”, and a few others, but always I get the same error.

It looks like a perfectly good address, so what would be valid in that
context.


Also, what I really want to do is just call either “receive” or
“beginreceive”, and start getting messages.  However, when I do that, I get
an exception that says I have to do a Bind operation before calling Receive.
The examples on MSDN don’t have a bind step, so I’m a bit confused about that
as well.
System.Net.IPAddress.Parse
(1)
System.Net.Sockets.UdpClient
(1)
System.Net.IPEndPoint
(1)
System.Net.IPAddress
(1)
IPEndPoint
(1)
IPAddress
(1)
Socket.ReceiveFrom
(1)
UdpClient.Receive
(1)
  Peter Duniho replied...
07-Feb-08 07:46 PM
An actual valid address.

I am unable to reproduce your problem.  Using code that does exactly what
the code you posted does, if I enter an IP address in the textbox that
matches my local IP address, that succeeds.  If I enter "0.0.0.0"
(essentially, the IPAddress.Any address), that succeeds.  If I enter an IP
address other than those two, I get the exception you're describing, but
then that's expected in that case.

So, I suspect that you are not entering a valid address, in spite of
whatever belief you have that you are.


I agree that the MSDN samples are not necessarily the best.  However, I
looked at three different pages, UdpClient.Receive(IPEndPoint),
Socket.Receive(byte[]), and Socket.ReceiveFrom(byte[], EndPoint), and in
each case the sample is either incomplete (so you would need to look
elsewhere for the initialization) or it _does_ bind the socket.
Specifically, the Socket.Receive(byte[]) page doesn't include an explicit
bind, but the documentation says right at the top that it's to be called
on a bound socket.  The other two pages do bind the object (UdpClient in
the first case, Socket in the second) in the sample code provided.

In other words, yes you do need to bind the socket, and the documentation
is actually reasonably clear on this, at least where I looked.  If you
found somewhere else that it's not clear, file a comment with a rating for
the page and hopefully Microsoft will fix it.

By the way, I'm a little confused as to why you're using the UdpClient
class.  You seem to be trying to get at the lower level, using the Socket
directly.  If you're going to do that, why not just create a UDP Socket
and use that?

Pete
  Pat Kujawa replied to Davi
11-Feb-10 02:05 PM
I had the same difficulty, but I found a workaround. If you create and Bind the Socket first, then assign the instance to your UdpClient.Client, you can achieve binding the UdpClient to your local endpoint and still use the UdpClient's convenient methods.

See http://snipplr.com/view/28192/bind-a-socket-including-udpclient-and-tcpclient-to-a-local-network-interface-card-nic/
  Pat Kujawa replied to Davi
11-Feb-10 02:06 PM
I had the same difficulty, but I found a workaround. If you create and Bind the Socket first, then assign the instance to your UdpClient.Client, you can achieve binding the UdpClient to your local endpoint and still use the UdpClient's convenient methods.

See http://snipplr.com/view/28192/bind-a-socket-including-udpclient-and-tcpclient-to-a-local-network-interface-card-nic/
Create New Account
help
vs little endian). We can correct that but it is = uglier: 85> gc ips.txt | %{[System.Net.IPAddress]::Parse($_)} | sort = {$bytes = 3D$_.GetAddressBytes();[array]::Reverse($bytes);[BitConverter]::T = oUInt32($bytes, 0)} | ft 7 If you did this a lot I would consider modifying the type data for = System.Net.IPAddress to add a BegEndianAddress property like so: Create an IPAddress.ps1xml file with these contents execute: 2> Update-TypeData IPAddress.ps1xml You've just added a new property to every System.Net.IPAddress object = that you will encounter in PowerShell. You might want to put the
this by hand = should I be forced to :) function iptolong { param([string]$ipaddress) $a = 3D [system.Net.IPAddress]::Parse($ipaddress) [byte[]]$b = 3D $a.GetAddressBytes() $longip = 3D 0 for ($i = 3D 0; $i -lt 256) * ([math]::pow(256, 3-$i))) + $longip } return $longip } = 20 function longtoip { param ($long) return [system.Net.IPAddress]::Parse($long) } = 20 function ToBinary { param($dec) return ([system.Convert]::ToString($dec, 2)).padleft(32, [char
don't really know how to do this with powershell. I tried: $multiep = New-Item System.Net.IPEndPoint([System.Net.IPAddress]::Parse("234.5.6.7"), 58432) $sock = New-Item System.Net.Sockets.Socket([System.Net.Sockets Sockets.SocketType]'Dgram', [System.Net.Sockets.ProtocolType]'Udp' ) $d = New-Item System.Net.Sockets.MulticastOption([System.Net.IPAddress]::Parse("234.5.6.7")) $iep = New-Item System.Net.IPEndPoint(System.Net