| 
                  UDP Socket Programming
                   An Introduction with Examples in C  | 
            
| 
                   
                      
                     Prof. David Bernstein
                       | 
            
| Computer Science Department | 
| bernstdh@jmu.edu | 
               
            
         
            
         
         
            
         sockaddr:
      
         
            
         ssize_t recvfrom(int fd, void *buffer, size_t buflen, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
               
                        fd
                      | 
                     The file descriptor of the UDP socket | 
                        buffer
                      | 
                     The buffer to fill with the datagram's payload | 
                        length
                      | 
                     The length of the buffer | 
                        flags
                      | 
                     Flags (often 0) | 
                        src_addr
                      | 
                     The address the datagram was sent from | 
                        addrlen
                      | 
                     The length of the source's address | 
| Return | The number of bytes received on success; -1 on error | 
         
            
         
                        
                     
         
            
         sockaddr:
      
         
            
         ssize_t sendto(int fd, const void *buffer, size_t buflen, int flags, const struct sockaddr *dest_addr, socklen_t *addrlen)
               
                        fd
                      | 
                     The file descriptor of the UDP socket | 
                        buffer
                      | 
                     The datagram's payload | 
                        length
                      | 
                     The length of the payload | 
                        flags
                      | 
                     Flags (often 0) | 
                        dest_addr
                      | 
                     The address to send the datagram to | 
                        addrlen
                      | 
                     The length of the destination address | 
| Return | The number of bytes sent on success; -1 on error | 
         
            
         
                        
                     
         
            
         
         
            
         
         
            
         
         
            
         
         
            
         
         
            
         int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
               
                        fd
                      | 
                     The file descriptor of the UDP socket | 
                        level
                      | 
                     SOL_SOCKET, IPPROTO_IP, ... | 
                        optname
                      | 
                     The option to set | 
                        optval
                      | 
                     The value of the option | 
                        optlen
                      | 
                     The size of the optval argument | 
| Return | 0 on success; -1 on error | 
         
            
         SO_SNDTIMEO and SO_RCVTIMEO
              allow you to set timeouts (i.e., how long a call will
              block before failing)SO_LINGER allows the socket to delay closing if
              there are data to sendSO_REUSEADDR and SO_REUSEPORT
              allow you to re-use an address and/or port even if they
              are already bound (which can be useful when a server
              fails and must be restarted)SO_RCVBUF and SO_SNDBUF allow you
              to change buffer sizes