Simple example of UDP packet program in c/c++ under Linux

Discussion in 'Trading Software' started by nitro, May 29, 2005.

  1. nitro


    I have been trying to port some software to linux that I have gotten to run under MSWindows. I can't for the life of me get a simple packet program to work under linux and I don't see why it doesn't work :confused:

    I have verified that tcpdump is getting packets from the address that I add membership to on the given interface in my program, and I am also able to get packets under the MSWindows program, so it is definetly my port of the program to linux that is at fault.

    Does someone know of a simple linux example program in C/C++ with source that shows how to do the simplest UDP multicast packet recvfrom?


    I havn't read through it so I'm not sure it's exactly what you want, but it may help.

    If you are doing any serious amount of Unix network programming buy Stevens book. I think it's called "Unix Network Programming". It's considered to be the best.
  3. nitro


    Thanks for the reply.

    Unfortunately, the example program that you give is a program that sends packets. I am already able to write a program that acts as a broadcast server and broadcast client under linux in C.

    What I can't do is write a Multicast program that uses recvfrom to get mc packet data.

    The simplest version of (something like) tcpdump is what I am looking for.

    Thanks for the Stevens suggestion. I have the book, but his stuff is so convoluted with his own syntatic sugar that it becomes a big pain in the ass to get even a simple program running.

  4. nitro


    _MUCH_ closer but still get nothing. Now, since I have more than one NIC card, I need to modify this program so that it knows to use eth1. So I have to modify the line that uses INADDR_ANY.

    Here is what the tcpdump command looks like:

    tcpdump -i eth1 udp port 50001

  5. I'm a bit rusty on this stuff, so if I'm talking rubbish then just ignore.

    If you have multiple interfaces, I think you should bind the socket to the specific interface not INADDR_ANY.
  6. nitro


    You are 100% correct. It is MADDENING and I am about to throw my computer out a third floor window.

    This SHOULD work but it doesn't. tcpdump happily prints packets while this program just sits there...

    :mad: nitro :mad:

    Thanks for the help - your program should work too :(
  7. I konw the feeling well at the moment. I've spent too many hours trying to get a Matrox G200 MMS working with X and a DVI flat panel.
  8. To simplify matters why don't you get rid of the other interface (s). ifconfig down should do it.
  9. nitro


    Can't - I am remotely connected to the machine. There are three interfaces for a reason:

    1) Public Internet address for remote access
    2) Local network (192.168.1.x) for private cluster
    3) Broadcast addresses (172.16.90.x) listens to -> (239.7.7.x)

    I am not conviced that is the problem though. I just changed the two lines that read:

    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_addr.s_addr = inet_addr("");
    On the command line I type:


    The program correctly reports that it is listening on for packets, but prints no incoming packets. Should work...

    #10     May 30, 2005