9

Its my first time programing network in java. I was looking for a way to send to somehow broadcast to all nodes in the whole networking. To let them know of my existence. I'm trying to make a multiplayer network game, and I want the clients to be able to see all the games available to choose which one to join. I want to know how to broadcast from the server and also how to make the clients listen.

Please make it simple, I'm a newbie :)

Thanks in advance.

Pro.Hessam
  • 819
  • 3
  • 11
  • 26

3 Answers3

6

To broadcast data packets, send them to the broadcast address of the given subnet (the last address of the subnet). The IP 255.255.255.255 is the broadcast address for the zero network.

A special definition exists for the IP broadcast address 255.255.255.255. It is the broadcast address of the zero network or 0.0.0.0, which in Internet Protocol standards stands for this network, i.e. the local network. Transmission to this address is limited by definition, in that it is never forwarded by the routers connecting the local network to the Internet.

Broadcast address

So to broadcast to your current network, send the packets to 255.255.255.255.

Community
  • 1
  • 1
Jacob
  • 41,721
  • 6
  • 79
  • 81
  • 1
    This is actually a bad idea, because UDP broadcasts consume excessive network resources, and most routers will not forward such packets without customizing the configuration to specifically allow it. – Michael Aaron Safyan Jul 04 '11 at 07:35
  • @Michael Did not know that most routers don't forward that. How does DHCP work then? It's just how i learned it and seen it in other SO answers/questiond, i.e. http://stackoverflow.com/questions/737899/broadcasting http://stackoverflow.com/questions/869925/testing-broadcasting-and-receiving-messages ... – Jacob Jul 04 '11 at 07:53
  • 1
    As the routers do not forward broadcast, only NICs in the LAN (an probably only in the subnet) will be reached. – SJuan76 Jul 04 '11 at 08:57
  • I thought he wanted to implement a multiplayer network game, so it should be sufficient if it works in the current subnet. – Jacob Jul 04 '11 at 09:03
  • 1
    @cularis, the broadcast will still reach the router, but the router won't forward it. It actually may not be sufficient even if it works in the current subnet. I learned this the hard way, because I was burned on a school project by implementing something with UDP broadcast. There were multiple routers at our school all using the same SSID, so even if multiple devices were connected to the same logical network, they could actually be talking to different routers, and the packets wouldn't reach other. – Michael Aaron Safyan Jul 04 '11 at 10:28
  • @cularis Thanks for your response. I did what you said but when I made a socket with the IP 255.255.255.255 an exception was thrown with the message "Permission Denied". Making the broadcasting work on a adhoc network is satisfying enough for me. – Pro.Hessam Jul 04 '11 at 13:05
  • @Pro.Hessam maybe you could start another question with the code or edit it in, with info what OS you are using, because it is getting a little cluttered here. – Jacob Jul 04 '11 at 13:28
  • @cularis OK. You can see my new post here: http://stackoverflow.com/questions/6572715/an-excpetion-throws-when-i-try-to-make-a-socket-to-255-255-255-255 – Pro.Hessam Jul 04 '11 at 14:23
2

Do not confuse terms.

Broadcast is usually used for UDP. UDP is unreliable in the sense that it does not check if all of the packets are received by the clients. Opening a lot of TCP connections to a lot of clients is not broadcast.

To have your clients listen to a port, you need to use ServerSocket and read it.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Actually I don't have the server's IP address to connect to it through a ServerSocket. I want to server broadcast its existence so I get its IP address. – Pro.Hessam Jul 04 '11 at 07:52
  • Then that it would be really broadcast, but as Michael Aaron Safyan has written in a comment, it will only work in the LAN because routers usually do not redirect broadcast (for good measure, imagine everybody's broadcasts being transmitted all over the internet and clogging "the tubes"). – SJuan76 Jul 04 '11 at 09:00
0

I recommend that you use PubSubHubbub or a similar protocol. Basically, you would have a "hub" to which you send the notification that you want to have "broadcasted". Each of the nodes subscribes to the topic, by providing a URL that the hub can invoke when new data has arrived. When the "hub" receives this broadcast, the hub contacts each subscription URL to let the node know there is new data.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200