Thread: sockets/udp: Hello world

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    sockets/udp: Hello world

    I'm trying to learn about udp and most of the examples I am looking at are a little complex. Can someone please show a link or paste some code for a 'hello world' sort of server-side-UDP script?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    All the network programming I've done is tcp, but AFAIK udp is pretty similar as far as the basic arrangement is concerned, except you do not connect() accept() or listen(). Can you set up and use an inet socket?

    If you're on linux, the GNU C ref manual has a fairly down to earth section on sockets including UDP (aka "datagrams"), google "16.10.3 Datagram Socket Example" and you'll find it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    30
    MK27, It's required study for class. But so is TCP, so a hello world example for tcp is good too. I'll look at Linux manual in the mean time.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The initially bewildering part about socket programming is setting up the socket and all the address structs and endianess issues.

    Looking at the docs, it appears that for UDP this is identical to setting up a tcp server socket -- that is, you create the socket with bind() -- but then you do not use listen(), you just wait using recvfrom() (which is datagram specific; with TCP you use recv).

    Be warned that if you use that GNU example they are creating a local socket. These are somewhat simpler than inet sockets, but you cannot connect two computers with them (they are meant for interprocess communication)! Also, they only exist on *nix systems. So you would want to replace the "make_named_socket" function in that example with something from 16.6 The Internet Namespace.

    If you are using linux tho, you may want to try the example as is first since local sockets do not involve any of those addressing or endianess issues.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 01:55 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM
  5. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM

Tags for this Thread