150 likes | 275 Views
Socket Programming (Cont.). Networking CS 3470, Section 1 Sarah Diesburg. Control Flow. TCP Server. socket(). bind(). Well-known port. TCP Client. listen(). Socket(). accept(). blocks until connection from client. connect(). Connection establishment. Data(request). send().
E N D
Socket Programming (Cont.) Networking CS 3470, Section 1 Sarah Diesburg
Control Flow TCP Server socket() bind() Well-known port TCP Client listen() Socket() accept() blocks until connection from client connect() Connection establishment Data(request) send() recv() process request Data(reply) send() recv() close() End-of-file notification read() close()
Byte Ordering Increasing memory address Address A+1 Address A Little-endian byte order High-order byte Low-order byte MSB 16-bit value LSB Big-endian byte order Low-order byte High-order byte Address A+1 Address A Increasing memory address
Implications of Byte Order • Unfortunately there is no standard between these two byte orderings and we encounter systems that use both formats • We refer to the byte ordering used by a given system as host byte order • The sender and the receiver must agree on the order in which the bytes of these multi-byte field transmitted: specify network byte order, which is big-endian byte ordering
Byte Order Functions #include <netinet.h> /* Host to network */ uint16_t htons(uint16_t host16bitvalue) Converts a 16-bit integer from host to network byte order uint32_t htonl(uint32_t host32bitvalue) Converts a 32-bit integer from host to network byte order Both return: value in network byte order /* Network to host */ uint16_t ntohs(uint16_t net16bitvalue) uint32_t ntohl(uint32_t net32bitvalue) Both return: value in host byte order
When do we use hton/ntohfunctions? • Use hton the port number in structsockaddr_in • If we create a custom struct to hold our headers and data • Sending our data through send() and recv() functions • E.g., if our first struct member is a 2-byte header, and sender/receiver have different memory orderings, number would look very different to each machine
Socket Address Structures #include <netinet/in.h> // Pointers to socket address structures are often cast to pointers // to this type before use in various functions and system calls: structsockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; // IPv4 AF_INET sockets: structsockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) structin_addrsin_addr; // see structin_addr, below char sin_zero[8]; // pad to fit into sockaddr }; structin_addr { uint32_t s_addr; // load with inet_pton() };
Address Conversion Functions #include <arpa/inet.h> intinet_pton(intaf, const char *src, void *dst); /* Returns 1 on success, < 1 on error */ Converts the character string src into a network address structure, then copies the network address structure to dst. intinet_aton(const char *strptr, structin_addr *addrptr); /* return 1 if string was valid,0 error */ Convert an IP address in string format (x.x.x.x) to the 32-bit packed binary format used in low-level network functions
Address Conversion Functions #include <arpa/inet.h> in_addr_tinet_addr(const char *strptr); /* return 32-bit binary network byte ordered IPv4 address; INADDR_NONE if error, deprecated and replaced by inet_aton() */ char *inet_ntoa(structin_addrinaddr); /* returns: pointer to dotted-decimal string */
Example structsockaddr_in ip4addr; int s; ip4addr.sin_family = AF_INET; ip4addr.sin_port = htons(3490); inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr); s = socket(PF_INET, SOCK_STREAM, 0); bind(s, (structsockaddr*)&ip4addr, sizeof ip4addr);
Man Pages • Use man pages to look up useful information • $> man cat • Get information about shell commands • $> man bind • Get information about C library/system calls • Also tells you which header files to include • $> man man • Get information about man pages
Man Pages • Sometimes you need to specify a man section • E.g., printf is both a shell command and a C library call • Use either • $>man 1 printf • $>man 3 printf • See http://www.cs.uni.edu/~diesburg/courses/cs3470_fa13/resources/man_page_levels.htm
C Header Files • Located at /usr/include • Can find out what functions are available for you to use and struct definitions • (Hint: check out <string.h> and <strings.h>)
Byte Manipulation Functions #include <strings.h> /* Berkeley-derived functions */ void bzero(void *dest, size_tnbytes) Set the first part of an object to null bytes void bcopy(const void *src, void *dest, size_tnbytes); intbcmp(const void *ptr1, const void *ptr2, size_tnbytes) /* return:0 if equal, nonzero if unequal */ #include <string.h> /* ANSI C defined functions */ void *memset(void *dest,intc,size_tlen) Sets the first len bytes in memory dest to the value of c void *memcpy(void *dest,const void *src, size_tnbytes) void memcmp(const void *ptr1, const void *ptr2, size_tnbytes)