Jump to content
  • Advertisement
Sign in to follow this  
Kurt69

UDP Broadcast

This topic is 4437 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi this is my first post here, sorry to start it off with a question :D. So yeah anyway I'm trying to implement a game listing into my program, my plan is to have someone in hosting mode send out a UDP broadcast of their game, while anyone at the game listing form has a UDPClient listening for the broadcast. Here is what I have tried for the broadcast code:
Quote:
Do While okToBroadcast = True Dim s1 As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) Dim broadcast As IPAddress = IPAddress.Parse("255.255.255.255") Dim sendbuf As Byte() = Encoding.ASCII.GetBytes("test") Dim ep As New IPEndPoint(broadcast, 11000) s1.Bind(ep) s1.SendTo(sendbuf, ep) rxtChat.AppendText(Chr(13) & "Broadcasting Game on Local Network.") Loop
But that generates this exception:
Quote:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll Additional information: The requested address is not valid in its context
Thanks in advanced for any help, it is very much appreciated as I have been getting VERY frustrated with this.

Share this post


Link to post
Share on other sites
Advertisement
I don't know Visual Basic, but I do know that you have to explicity enable the broadcast option (SO_BROADCAST) on UDP sockets before you're allowed to broadcast on sockets.

You also can't bind to the broadcast address; you have to bind to the "any" address (which is 0.0.0.0). That's probably why you're getting the exception.

To debug this further, I would recommend that you turn on exception catching in DEVENV, such that the code stops when an exception is thrown. That will show you exactly what statement is having the problem. (Stepping through the code would do almost the same thing, too)

Share this post


Link to post
Share on other sites
Ok so here's my updated code along with the next error I get.

Quote:
Try
Do While okToBroadcast = True
Dim s1 As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim broadcast As IPAddress = IPAddress.Parse("255.255.255.255")
Dim sendbuf As Byte() = Encoding.ASCII.GetBytes("test")
Dim ep As New IPEndPoint(broadcast, 11000)
Dim ep2 As New IPEndPoint(IPAddress.Any, 11000)
s1.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1)
s1.Bind(ep)
s1.SendTo(sendbuf, ep)
rxtChat.AppendText(Chr(13) & "Broadcasting Game on Local Network.")
Loop
Finally
End Try


Quote:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll

Additional information: The requested address is not valid in its context


However if i do "s1.Bind(ep2)" I get this error.
Quote:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll

Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted


[Edited by - Kurt69 on June 28, 2006 7:36:33 AM]

Share this post


Link to post
Share on other sites
Did you step through the code one line at a time to see which line has the problem?

Did you read the documentation for that particular call, to see what it says about its arguments?

Did you follow The Debugging Article?

And, last, you should bind once, to 0.0.0.0:port, turn on the broadcast option, and then use SendTo() with 255.255.255.255:port to broadcast from the bound socket (which you don't do). You shouldn't allocate the socket more than once (which you do).

Share this post


Link to post
Share on other sites
I have done what you're saying haven't I? Anyway I commented the BindTo line out and it works now.

The code:
Quote:
Try
Do While okToBroadcast = True
Dim s1 As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim broadcast As IPAddress = IPAddress.Parse("255.255.255.255")
Dim sendbuf As Byte() = Encoding.ASCII.GetBytes("test")
Dim ep As New IPEndPoint(broadcast, 11000)
s1.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1)
's1.Bind(ep)
s1.SendTo(sendbuf, ep)
rxtChat.AppendText(Chr(13) & "Broadcasting Game on Local Network.")
Loop
Finally
End Try

Thanks for your help btw, at least I got it solved :D (rating points for you)

But yeah I have a problem with the code that receives the broadcast, you see if you goto the form once it will work perfectly, if you hide the form then show it again it generates this exception:

Quote:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in system.dll

Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted


I know it's because each time it shows the form it tries to create the socket but it hasnt been disposed of, thing is I don't know how to dispose it properly. I've tried mySocket.Close() when the form is closed/hidden, but it doesn't work, please help.

Edit: I did some testing and it seems this exception does not matter, the udpclient socket will still receive the broadcast, so I just Catch it now and do nothing for it.

[Edited by - Kurt69 on June 30, 2006 1:27:41 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!