130 likes | 262 Vues
This guide delves into UDP programming with a focus on socket address structures, including IPv4 basics, byte ordering, and essential functions such as socket(), bind(), sendto(), recvfrom(), and close(). It covers the sockaddr_in structure for IPv4, introduces generic socket structures for varying protocol families, and discusses the significance of byte ordering in network communication. In addition, the text explains address conversion functions, providing insights into creating, binding, sending, and receiving data over UDP sockets. A must-read for aspiring network programmers.
E N D
Programming with UDP – I CoveredSubjects: IPv4 SocketAddressStructure ByteOrderingFunctions Address Access/ConversionFunctions Functions: socket() bind() sendto() recvfrom() close()
SocketAddressStructures • Most socket functions require a pointer to a socket address structure as an argument. • Each supported protocol defines its own socket address structure. • Thenames of thesestructuresbeginwithsockaddr_andendwith a uniquesuffixforeachprotocolsuite.
IPv4 SocketAddressStructure • Internet socketaddressstructure is namedsockaddr_inanddefined in <netinet/in.h> • IP address: structin_addr { in_addr_ts_addr; /* 32-bit IP address */ }; /* network byteordered*/ • TCP or UDP Address: structsockaddr_in { uint8_t sin_len; /* length of structure*/ shortsin_family; /* e.g., AF_INET */ ushortsin_port; /* TCP/UDP port */ structin_addr; /* IP address */ charsin_zero[8]; /* unused */ }; Note: Havingsin_lensimplifieshandlingvariablelengthsocketaddressstructures. All but sin_familyin network byteorder
GenericSocketAddressStructures Problem: • A socket address is always passed by reference when passed as an argument to socket functions. • But anysocketcallthattakesone of thesepointers as an argumentmustdealwithsocketaddressstructuresfrom ANY of thesupportedprotocolfamilies. • A problem arises in how todeclarethetype of pointerthat is passed.
continued.. Solution: • is to define a genericsocketstructuredefined in <sys/socket.h> Example: intbind(int, structsockaddr *, socklen_t); structsockaddr_inserv; /* IPv4 address */ bind(sockfd, (structsockaddr *)&serv,sizeof(serv)); Ifweomitthiswarningcomes! Q: Why not do (void *)? structsockaddr{ uint8_t sa_len; sa_family_tsa_family; /* AF_INET, AF_INET6 .. */ charsa_data[14] /* protocolspecificaddr */ };
ByteOrdering • Big Endian vs. Little Endian • LittleEndian (Intel, DEC): • Least significant byte of word is stored in the lowestmemoryaddress • Big Endian (Sun, SGI, HP): • Most significant byte of word is stored in the lowestmemoryaddress • Network Byte Order = Big Endian • Allows both sides to communicate • Must be used for some data (i.e. IP Addresses) • Good form for all binary data
ByteOrderingFunctions • 16- and 32-bit conversion functions (for platformindependence) • Examples: int m, n; shortint s,t; m = ntohl (n) net-to-host long (32-bit) translation s = ntohs (t) net-to-host short (16-bit) translation n = htonl (m) host-to-net long (32-bit) translation t = htons (s) host-to-net short (16-bit) translation
Address Access/ConversionFunctions in_addr_tinet_addr(const char* strptr); • Translate dotted-decimal notation to IP address; returns -1 onfailure, thus cannot handle broadcast value “255.255.255.255” intinet_aton(const char *strptr, struct in_addr *inaddr); • Translate dotted-decimal notation to IP address; returns 1 onsuccess, 0 on failure. char* inet_ntoa(structin_addrinaddr); • Translate IP address to ASCII dotted-decimal notation (e.g., “128.32.36.37”)
socket()Function • Create a socket • intsocket(intfamily, inttype, intprotocol); • Returns file descriptor or -1. • Fields: • intfamily • AF_INET IPv4 protocols • AF_INET6 IPv6 protocols • inttype • SOCK_DGRAM for UDP datagrams • SOCK_STREAM for TCP streams • intprotocol(forAF_INET & AF_INET6familysockets) • IPPROTO_UDP UDP Transport • IPPROTO_TCP TCP Transport
bind()Function • Bind a socket to a local IP address and port number • intbind (intsockfd, structsockaddr* myaddr, intaddrlen); • Returns 0 if OK or -1 on error. • sockfd:socket file descriptor (returned from socket) • myaddr: includes IP address and port number • IP address: set by kernel if value passed is INADDR_ANY, else set bycaller • port number: set by kernel if value passed is 0, else set bycaller • addrlen: length of address structure • = sizeof (structsockaddr_in)
sendto() & recvfrom()Functions intsendto (intsockfd, char* buf, size_t nbytes, intflags, structsockaddr* destaddr, intaddrlen); • Send a datagram to another UDP socket. • Returns number of bytes written or -1. intrecvfrom (intsockfd, char* buf, size_t nbytes, intflags, structsockaddr* srcaddr, int* addrlen); • Read a datagram from a UDP socket. • Returns number of bytes read or -1.
close()Function intclose (intsockfd); • Close a socket. • Returns 0 on success, -1 on failure. • sockfd:socket file descriptor (returned from socket) • Closes communication on socket in both directions. • All data sent before closeare delivered to other side. • After close, sockfdis not valid for reading orwriting.