8.2. Creating a socket for use with UDP

Ya know, there's nothing like a good cheeseburger.

Like TCP, UDP programming requires the use of a socket. You'll need to call the socket() API to get one.

Before we can do that, we'll need to add a new constant to our SOCKET_H member:

        D SOCK_DGRAM      C                   CONST(2)
     

This tells the socket() API that you want a datagram socket instead of a TCP socket.

Now, we can call the socket() API like this:

        C                   eval      s = socket(AF_INET:SOCK_DGRAM:IPPROTO_IP)
        C                   if        s < 0
        C**  error occurred, check errno
        C                   endif
     

As you can see, we still tell the socket() API that it's going to use the "INET" (internet) address family. And that it's going to use the "IP" (Internet Protocol) In TCP, we send our data in a stream, but in UDP, we'll use datagrams, thus we tell it "SOCK_DGRAM" instead of "SOCK_STREAM".

Makes a lot more sense than that cheeseburger remark, doesn't it?