Trouble binding a UDPClient

D

David

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.
 
P

Peter Duniho

[...]
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.

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.
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.

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
 
P

Pat Kujawa

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-...client-to-a-local-network-interface-card-nic/



Davi wrote:

Trouble binding a UDPClient
07-Feb-08

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 contex

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.

Previous Posts In This Thread:

Trouble binding a UDPClient
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 contex

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.

Re: Trouble binding a UDPClient

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


Submitted via EggHeadCafe - Software Developer Portal of Choice
Putting Twitter Realtime Search to Work
http://www.eggheadcafe.com/tutorial...24-c9960b55b669/putting-twitter-realtime.aspx
 
P

Pat Kujawa

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-...client-to-a-local-network-interface-card-nic/



Davi wrote:

Trouble binding a UDPClient
07-Feb-08

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 contex

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.

Previous Posts In This Thread:

Trouble binding a UDPClient
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 contex

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.

Re: Trouble binding a UDPClient

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

Re: Trouble binding a UDPClient
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-...client-to-a-local-network-interface-card-nic/


Submitted via EggHeadCafe - Software Developer Portal of Choice
Build a Selected Text Favorites Utility for your Web Site
http://www.eggheadcafe.com/tutorial...c-86feb39cae83/build-a-selected-text-fav.aspx
 

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