Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Back in the IPv4 days, people simply used broadcast packets check if there are any server available in the network as described in this answer. But in the IPv6 protocol they've dropped broadcast support. There is still multicast support available but how can/should I use it?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

With broadcast the client sends a message to everybody on the network and all the servers reply. With multicast you define a multicast group address and all the servers subscribe to it. The client then sends a message to the group, the servers that have subscribed receive it and reply.

Multicast is for when one sender wants to send to a group of receivers, like when a game client wants to send to a (potential) group of servers. Multicast addresses are special addresses where the system knows to treat them differently. The receiver tells the system that it wants to receive messages sent to a particular group, and the sender sends a message to the group address. On a LAN this just works. Across LANs you need multicast routing which isn't implemented on mostnetworks. But broadcast wouldn't work across LANs either.

Using multicast makes sure that only the systems on the network that care about getting the message will receive it.

How you implement this depends on the programming language etc.

The structure of an IPv6 multicast address is as follows:

  • It always starts with the first 8 bits of the address set to 1, which means that the first two characters of the address will be ff;
  • The 3rd character (bits 9 to 12) in the address specifies flags. In your case you probably want a fixed multicast address for your application. In that case the 3rd character will be a 0 which indicates a permanently assigned multicast address;
  • The 4th character (bits 13 to 16) determine the scope of the address. You will very probably Use the value 2 for link-local (LAN) scope.

Together this means that you'll use an address starting with ff02:.

Multicast addresses are assigned by IANA. RFC3307 defines how to do it (the criteria is Expert Review, so it's not necessary to write an RFC on what you are doing or anything like that). In this answer I'll use the multicast address ff02::db8:aa:bb, which is in the block reserved for documentation.

You don't have to have root access to use multicast. The following Python3.3 examples can be run with a normal user account:

The server (listening on the multicast address):

#!/usr/bin/env python3.3
import socket
import struct

if_idx = socket.if_nametoindex('en0')
addr = 'ff02::db8:aa:bb'
port = 42424
group = socket.inet_pton(socket.AF_INET6, addr) + struct.pack("I", if_idx)

sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, group)
sock.bind(('::', port))

while True:
  msg, sender = sock.recvfrom(1024)
  print('Received "%s" from "%s"' % (str(msg, 'UTF-8'), sender))
  sock.sendto(bytes('Received %d bytes from you' % len(msg), 'UTF-8'), sender)

And the client (sending to the multicast group and listening for the replies):

#!/usr/bin/env python3.3
import socket

if_idx = socket.if_nametoindex('en0')
addr = 'ff02::db8:aa:bb'
port = 42424

sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, if_idx)

sock.sendto(bytes('Hello there!', 'UTF-8'), (addr, port))
while True:
  # You probably wait a certain time for replies, not indefinitely like this example
  msg, sender = sock.recvfrom(1024)
  print('Received "%s" from "%s"' % (str(msg, 'UTF-8'), sender))

I used Python 3.3 because older versions don't have socket.if_nametoindex, but everything else should also work in Python 2.

PS: using an existing library or framework for service discovery as suggested in another answer is a good idea. It will use multicast under the hood but save you from having to design and implement your own protocol.

share|improve this answer
    
don't you need administrative permissions to create/join a group? and besides how does this group thing work? I mean do I choose the group address when I want to create/join one? –  Ali.S Dec 23 '12 at 5:05
    
I'll expand the answer to include some more details –  Sander Steffann Dec 24 '12 at 14:25
    
This answer is probably technically right but it misses a bit of a explanation of how multicast works. Do I understand it correctly that you can create a multicast socket and then everybody in the network knows where this socket is? –  API-Beast Dec 26 '12 at 21:06
    
Also if so, how is it differentiated which sockets are in the local network, and which are outside, like the internet. –  API-Beast Dec 26 '12 at 21:13
    
Expanded answer –  Sander Steffann Dec 28 '12 at 9:34

There are protocols for service discovery which modern applications should use instead of broadcast or home-spun multicast solutions, whether you're on IPv4 or IPv6.

Apple pushes mDNS/DNS-SD and Microsoft pushes UPnP. Both accomplish the same goals for simple service discovery, while UPnP offers many additional features.

There are freely available libraries for both of those APIs for a multiple platforms. The relevant OSes include native support. Linux offers support via semi-standard system components installed by default on most distros.

Note that UPnP can also be used for firewall configuration and hence may be the better choice for games that plan on having Internet play, although you can't rely on or require UPnP firewall control since many users don't have compatible routers or turn the feature off out of paranoia.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.