Saturday, March 16, 2013

Linux UDP Multicast

Setting up a UDP multicast server/client in Linux should be pretty straightforward. An example is provided below with some comments.

Get the files hosted at using the link below:
UDP sender/listener example

UDP Multicast Sender in Linux (C/C++)
(udp_sender.cxx - see the link above)
  1. Create a socket

  2. This line creates a UDP connectionless socket
    fd = socket( AF_INET, SOCK_DGRAM, 0 ) ;
  3. Fill in information about the interface you will use

  4. These lines fill a data structure that provide out going interface address and port number.
    if_addr.sin_family = AF_INET ; if_addr.sin_addr.s_addr = inet_addr( INTERFACE_ADDR ) ; if_addr.sin_port = htons( INTERFACE_PORT ) ;
  5. Bind the socket to the interface

  6. This line binds the interface address/port number to the socket that was just created.
    bind( fd, (struct sockaddr*) &if_addr, sizeof( if_addr ) ) ;
  7. Fill in information about the multicast group you will use

  8. Fill in another data structure that provides information about the destination multicast address and port number.
    mc_addr.sin_family = AF_INET ; mc_addr.sin_addr.s_addr = inet_addr( MULTICAST_ADDR ) ; mc_addr.sin_port = htons( MULTICAST_PORT ) ;
  9. Send the data

  10. The sendto() command will transmit the data out of the socket and onto the network with the multicast destination address.
    result = sendto( fd,
    sizeof( msg ),
    (struct sockaddr *) &mc_addr,
    sizeof( mc_addr ) ) ;

UDP Multicast Listener in Linux (C/C++)
(udp_listener.cxx - see the link above)
  1. Create a socket

  2. Create a UDP connectionless socket...add non-blocking reads
    fd = socket( AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0 ) ;

  3. Fill in information about the interface you will use

  4. In this case the listener can listen on any interface as long as the multicast port is correct.
    if_addr.sin_family = AF_INET ; if_addr.sin_addr.s_addr = htonl( INADDR_ANY ) ; if_addr.sin_port = htons( MULTICAST_PORT ) ;

  5. Bind the socket to the interface

  6. Bind the socket to the 'interface'
    bind( fd, (struct sockaddr *) &if_addr, sizeof( if_addr ) ) ;

  7. Request to join the multicast group

  8. This is the important you indicate the multicast address you want to listen to and the interface that will listen for the multicast.
    mreq.imr_multiaddr.s_addr = inet_addr( MULTICAST_ADDR ) ; mreq.imr_interface.s_addr = inet_addr( INTERFACE_ADDR ) ; ret_val = setsockopt( fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof( mreq ) ) ;

  9. Receive data

  10. The recvfrom() function will read multicast data off the socket.
    recv_bytes = recvfrom( fd,
    (struct sockaddr *) &if_addr,
    (socklen_t *) &addrlen ) ;

Saturday, March 2, 2013

Installing a Linux Virtual Machine

Need to run Linux, but all you have is a Windows or Mac desktop/laptop? You could go through the hassle of setting up your system to dual boot by setting up separate drives or drive partitions. That is one solution, but there is another way that allows you to keep your original system unmolested while allowing you the flexibility to add and remove operating systems with relative ease. A virtual machine is like a simulated system that runs like a normal computer, except that is exists completely in software. Virtual machines are very useful when you want to run multiple operating systems (or different operating systems) on the same hardware at the same time. Each OS 'thinks' its running on its own computer so nothing special needs to be done with the OS for it to operate. The only thing you may need to do is set up the VM software to give the OS access the network, CD drives, etc.

Note on terminology:
Host OS - This is the operating system that the virtual machine software is installed on. For example, I have a Windows laptop, so my host OS is Windows 7.
Guest OS - This is the operating system that is installed on a virtual machine. For example, I want to have a virtual machine that runs a Linux OS, so my guest OS is Linux.

Steps for installing and setting up a Virtual Machine:
  1. Install VM software
  2. Configure a VM
  3. Install OS
  4. Apply updates and configurations

1. Install VM software
There are many kinds of VM software available. Some are free and provide a lot of the basic functions you need to get started. Others cost money and offer a lot of features that are geared towards business. In this case, I chose VirtualBox as my VM. It's free, offers the features I need and can be installed on Windows, Mac, Linux and Solaris host operating systems.
I want to use a virtual machine that runs a Linux OS. I have a Windows laptop, so I installed VirtualBox for Windows hosts. Make sure you install the right version that matches your host OS and CPU architecture (32-bit or 64-bit).

2. Configure a VM
Once the VM software has been installed, you should be able to configure a virtual machine for your guest OS. I want to install CentOS 6.3, which is a Linux flavor based upon the popular RedHat Linux OS. Make sure to look up the minimum hardware requirements for your guest OS. Your virtual machine should be configured to 'give' enough RAM and hard drive space for your guest OS to function. In this case, VirtualBox allows you to tell it what type of guest OS you are using so it suggests that I allow CentOS to have 512 MB of RAM and give it up to 8GB of hard drive space.
VirtualBox overview window
Virtual Machine Overview in VirtualBox

3. Install OS
There are a few ways you can install your guest OS. You can use an actual disk that is inserted into your CD/DVD drive, or you can use a disk image that was downloaded from the Internet. In both cases, you probably have to tell your VM software to allow your Linux virtual machine to access the disk or disk image. In this case, I used an ISO disk image to act as if a real CD was inserted into the virtual machines disk drive.
VirtualBox - Setting up thr CD drive
Setting up the CD drive for the Virtual Machine in VirtualBox

Start up the virtual machine and install the guest OS according to relevant documentation.

4. Apply updates and configurations
Once your guest OS is installed, you may need to configure the OS for networking or to handle that abstracted hardware components you gave it. Most of the work is probably done with the VM software. For example, your VM software may have some sort of networking component that allows the guest OS share your host OS's Internet connection. To your guest OS, all it sees is a typical Internet connection, and it knows nothing about your host OS setup.

If you have any questions on setting up your virtual machine, put a note in the comments and I, or someone else, may be able to help you.

Friday, March 1, 2013


Hi, my name is Justin, and I created this blog as a way to keep track of things I have learned that may be useful to me (or someone) in the future. Topics that may be covered include program examples, designs or other useful things that come up. There have been many times that I figured out how to do something, forgot what I did, and then needed to do it again later.
So, consider this blog a notepad that I use to write down stuff that I might need again in the future. It is also my hope that other folks out on the interwebs will benefit from the information here as well.