Sockets tutorial, datagram sockets example not working for me

This is a discussion on Sockets tutorial, datagram sockets example not working for me within the Networking/Device Communication forums, part of the General Programming Boards category; I'm learning C net programming from Beej's guide to network programming . Great guide. But two the example programs listener.c ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    9

    Sockets tutorial, datagram sockets example not working for me

    I'm learning C net programming from Beej's guide to network programming. Great guide.

    But two the example programs listener.c and talker.c do not work as expected on my machines. These are simple programs to demonstrate datagram sockets; listener listens on a port, talker sends a packet to that port.

    The code is from this section of the guide.

    I've tried running the two programs on the same machine and on seperate machines, both times the listener never gets a packet and just hangs there.

    I run the listener program first, then from another machine I run the talker program like this:
    Code:
    talker 10.1.1.5 hello
    but the listener NEVER gets the packet. What could be going wrong? Would anyone be good enough to run the code on their machine(s) and see if it works for them?

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I do a lot of network programming and would be glad to help but you need to post some more information. First if you are building the source exactly as posted in listener.c and talker.c, what compiler/platform are you using? What do your project files look like? In this case I am looking for at what libraries you are linking...also I have seen on other platforms (non-Linux/Windows) where if you don't link the right library the compiler will just happily allow the code to run off into the bit-bucket (to coin a phrase), emitting no errors, warnings and the code will try to run and silently fail.

    I have not compiled the stuff from Beej in a while so maybe something has changed...will rebuild and test...

    Peace
    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Also what is the IP address of the "talker" and the "listener"? When run on the same machine and using 127.0.0.1 does it work? Just hitting the obvious stuff first...UDP is about the simplest stuff in the universe to get going so we should have this sorted in no time...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    OK I just tried the two apps on a local machine, here is what I used to build and how it worked for me:
    Code:
    jeff@jeff-gate:~/dev/beej/talker$ gcc ./talker.c  -o talker
    jeff@jeff-gate:~/dev/beej/talker$ ./talker 127.0.0.1 hello
    talker: sent 5 bytes to 127.0.0.1
    jeff@jeff-gate:~/dev/beej/talker$
    and the listener:
    Code:
    jeff@jeff-gate:~$ cd dev/beej/listener/
    jeff@jeff-gate:~/dev/beej/listener$ gcc ./listener.c -o listener
    jeff@jeff-gate:~/dev/beej/listener$ ./listener 
    listener: waiting to recvfrom...
    listener: got packet from 127.0.0.1
    listener: packet is 5 bytes long
    listener: packet contains "hello"
    jeff@jeff-gate:~/dev/beej/listener$
    Seems to work. I will throw one across the network to make sure it works machine to machine. One thing came to mind while working on this was this: do you have a firewall of ANY kind (software or hardware) running inbetween the two machines? I think Windows comes with one turned on by default IIRC. The above code BTW was compiled and ran on a Linux box running kernel:
    Code:
    jeff@jeff-gate:~/dev/beej/listener$ uname -a
    Linux jeff-gate 2.6.28-17-generic #58-Ubuntu SMP Tue Dec 1 18:57:07 UTC 2009 i686 GNU/Linux
    jeff@jeff-gate:~/dev/beej/listener$
    BIAB..
    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    OK not going to bore you with another code paste; process worked just fine. listener machine was @192.168.0.166 so ./talker 192.168.0.166 hello emitted:
    Code:
    jeff@jeffhpdev:~$ ./listener 
    listener: waiting to recvfrom...
    listener: got packet from 192.168.0.175
    listener: packet is 5 bytes long
    listener: packet contains "hello"
    jeff@jeffhpdev:~$
    At this point I would look at what OS you are running, what if any firewalls are in place, etc. I think the next step that will give you the most information will be the 127.0.0.1 (local machine) test. That will remove firewalls and other OS-specific stuff from the equation. Unless you are running a Mac....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Thanks for the replies!

    Code:
    > uname -a
    FreeBSD blackbox.hansons 8.0-RC1 FreeBSD 8.0-RC1 #3: Wed Sep 23 17:34:43 NZST 2009     ben@blackbox.hansons:/usr/obj/usr/src/sys/BLACKBOX  i386
    >
    So OS shouldn't be a problem.

    I've ran the EXACT example code on the same machine with two terminals but nothing is received.

    Code:
    > ./talker 127.0.0.1 hello
    talker: sent 5 bytes to 127.0.0.1
    > ./talker hh hello
    ^C
    > ./talker localhost hello
    talker: sent 5 bytes to localhost
    >
    (I ran the middle command just to convince myself it wouldn't return properly if sending to a non-existent host...)

    But listener just sits there...
    Code:
    > ./listener
    listener: waiting to recvfrom...
    This is really frustrating... I don't see what could be going wrong. AFAIK there's no firewall.

    edit: sorry, forgot to mention, compiling with gcc, no special options, just gcc -o talker talker.c
    Last edited by caesius; 12-20-2009 at 12:44 PM.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Ah FreeBSD...I know very little about it other than there are some subtle differences between it and vanilla Linux. In any event the next thing I would try is this:
    1. Run listener
    2. In another term run netstat -a | grep <your listening port number>

    And see if the listener is actually listening or just silently failing...

    Also what about the firewall question? that might be eating your packets...just a thought...

    J.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Doesn't work on FreeBSD 8.0-RC3 either. I tried even cat /etc/passwd | nc -u 127.0.0.1 4950 but listener doesn't receive anything. I will try it tomorrow on FreeBSD 8.0-RELEASE after I have installed it (got it already compiled). I don't have time to check the code right now, I may look at it tomorrow or some other day.

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Interesting....I will be very interested in the eventual resolution to this since a lot of my work is on *nix platforms and are networking applications....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    @Fronty (or whoever): The more I think on this the more interested I become. Thus I am tempted to create a VM and install FreeBSD on it and troubleshoot the problem. I know, probably more effort than it is worth but on the other hand since (from what I hear) Linux got its networking stack from BSD there is no reason for it not to work the same so like a splinter in the mind, I gotta know ^__^

    With that in mind and knowing I have little to no *BSD experience:
    1. What version would you suggest I try?
    2. Given that I will be using VirtualBox, are there any tips that you would have to share?

    At first I was pretty sure it was an internal firewall problem but given that two folks have the same results I am no longer that certain....since so much of my work is cross-platform this is the kind of thing that I need to know the answer to.....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Nice to hear I'm not the only one having the problem.

    @jeffcob, thanks for taking the time to reply and help solve this problem.

    I'd go for FreeBSD 8.0-RELEASE. But have no experience running from within VB.

    As for your suggestion above, I've run the commands and have the output:
    Code:
    > netstat -a | grep 4950
    
    
    udp6       0      0 *.4950                 *.*
    >
    I can't make sense of it however.

    Cheers

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Solved by someone from the FreeBSD forums. To quote them,

    <--

    What's going wrong here is that the listener is listening on a udp6 socket, but the talker sends on a udp4 socket.

    If you change this line in listener.c:

    Code:
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    to use AF_INET instead of AF_UNSPEC, then everything works fine.

    -->

    I still don't see why this works in Linux but not FreeBSD. Anyway, thanks for all replies.

  13. #13
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Second try at a reply; DB here ate my first one (or Chrome got into a fight with it and lost but anyways I was just thinking that maybe it is as simple as a default set to IPv4 in Linux and IPv6 in FreeBSD since IPv6 is the coming thing (according to the industry press). Admittedly I know very little about BSD (very at home with building a Linux kernel but get that "deer in the headlights" look when trying to partition a BSD drive) so anything is possible. If nothing else, we *all* come away from this with knowledge and that is nothing but a cool thing ^__^
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  14. #14

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Interesting. You should probably let Beej know about this. Actually, maybe he/she already does:
    And you should see listener responding that it got the packets. (If listener doesn't respond, it could be because it's bound to an IPv6 address. Try changing the AF_UNSPEC in listener.c to AF_INET to force IPv4.)
    (That's in an unrelated section, though.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winprog BMP tutorial not working
    By Brigs76 in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2005, 07:36 PM
  2. Anyone know a good tutorial on windows sockets?
    By Xoid in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 08:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21