;

Client/Server Examples for Multicast

The internet seems to be missing a complete and working example of how to code multicast servers and clients so here it is. If you run any of these back to back and get a message about "address already in use" or related, remember the TIME_WAIT that occurs when you close a socket. It is OS dependent but a common default is 60 seconds. The group address is in the range 224.0.0.0 to 239.255.255.255. These addreses are the only ones valid for multicast use. More information on group address can be found in RFC 5771. Since you have to bind the client to an address and port you can only have one client with the same IP/Port specs running on a particular computer. An important fact to note is that multicast packets will not survive a trip thru most routers. The router will discard them. There are some home routers that have an option to pass multicast packets. The last tab on this page has a community built list of these routers along with the router web interface page the option can be found on. Multicast packets will go thru a switch however.


 
Contact Us

PHP

The Clients


   $len = 80 ;
   $flags = 0 ;
   $from = "" ;
   $port = 14446 ;
   $grpparms = array("group"=>"228.5.6.7","interface"=>"eth0") ;
   $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
   socket_bind($sock,'0.0.0.0',$port);
   socket_set_option($sock,IPPROTO_IP,MCAST_JOIN_GROUP,grpparms);
   socket_recvfrom($sock,$recvStr,$len,$flags,$from,$port);
   echo "Received " . $recvStr . " from " . $from ; 
   socket_close($sock) ;

The Server


   $port = 14446 ;
   $grpparms = array("group"=>"228.5.6.7","interface"=>"eth0") ;
   $mesg = "Eh, Whats up doc?" ;
   $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
   socket_set_option($sock,IPPROTO_IP,MCAST_JOIN_GROUP,$grpparms);
   socket_sendto($sock, $mesg, strlen($broadcast_string), 0, '228.5.6.7',$port);
   socket_close($sock) ; 


 
Contact Us

Java

The Clients


package client;

import java.net.*;

/**
 *
 * @author Buddy Bell
 */
public class Client {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        InetAddress group;
        DatagramSocket statsSock;
        try {
            statsSock = new MulticastSocket(4447);
            group = InetAddress.getByName("228.5.6.7");
            byte[] b = new byte[256];
            DatagramPacket dgram = new DatagramPacket(b, b.length);
            MulticastSocket socket = new MulticastSocket(4447); // must bind receive side
            socket.joinGroup(InetAddress.getByName("228.5.6.7"));

            while (true) {
                socket.receive(dgram); // blocks until a datagram is received
                byte[] bufB = new byte[dgram.getLength()];
                byte[] bufA = dgram.getData();
                System.arraycopy(bufA, 0, bufB, 0, bufB.length);
                String bufStr = new String(bufB);
                System.out.println("WEB   " + bufStr);
                dgram.setLength(b.length); // must reset length field!
            }
        } catch (Exception d) {
            d.printStackTrace(System.out);
        }
    }
}



The Server


package sendmulti;

import java.net.*;

/**
 *
 * @author Buddy Bell 
 */
public class SendMulti {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        DatagramSocket statsSock;
        DatagramPacket packet;
        InetAddress group;
        try {
            statsSock = new MulticastSocket(4488);
            group = InetAddress.getByName("228.5.6.7");
            byte[] pckt = new String("Test This Out").getBytes();
            packet = new DatagramPacket(pckt, pckt.length, group, 4447);
            statsSock.send(packet);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

}



 
Contact Us

C

The Clients


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>


#define port 4446
#define group "228.5.6.7"
#define bufSize 128

main(int argc, char *argv[])
{
     struct sockaddr_in addr;
     int fd, nbytes,addrlen;
     struct ip_mreq mreq;
     char msgbuf[bufSize];

     if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
	  perror("socket");
	  exit(1);
     }

     memset(&addr,0,sizeof(addr));
     addr.sin_family=AF_INET;
     addr.sin_addr.s_addr=htonl(INADDR_ANY);
     addr.sin_port=htons(port);
     
     if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
	  perror("bind");
	  exit(1);
     }
     
     mreq.imr_multiaddr.s_addr=inet_addr(group);
     mreq.imr_interface.s_addr=htonl(INADDR_ANY);
     if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
	  perror("setsockopt");
	  exit(1);
     }

	  addrlen=sizeof(addr);
	  if ((nbytes=recvfrom(fd,msgbuf,bufSize,0,
			       (struct sockaddr *) &addr,&addrlen)) < 0) {
	       perror("receive");
	       exit(1);
	  }
	  puts(message);
     }
}

The Server


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>

#define port 4446
#define group "228.5.6.7"

main(int argc, char *argv[])
{
     struct sockaddr_in addr;
     int fd, cnt;
     struct ip_mreq mreq;
     char *message="Hello from multicast land";

     if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
	  perror("socket");
	  exit(1);
     }

     memset(&addr,0,sizeof(addr));
     addr.sin_family=AF_INET;
     addr.sin_addr.s_addr=inet_addr(group);
     addr.sin_port=htons(port);
     
	  if (sendto(fd,message,sizeof(message),0,(struct sockaddr *) &addr,
		     sizeof(addr)) < 0) {
	       perror("send");
	       exit(1);
	  }
}



 
Contact Us

Python

The Clients


import socket
import struct

group = '228.5.6.7'
port = 4446

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((group, port))
request = struct.pack("4sl", socket.inet_aton(group), socket.INADDR_ANY)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, request)

print sock.recv(128)

The Server


import socket

group = '228.5.6.7'
port = 4446

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto("Hello from multicast land", (group, port))


 
Contact Us

Routers with Multicast

Help me build a database of router multicast passthru information

To prevent spam rather than a form please email me your:

Router Brand
Router Model
URL of router web interface page that has the multicast controls minus the IP address
Title of the checkbox, button, whatever, that refers to multicast passthru

 
Contact Us