Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Programming (Cont.)

Similar presentations


Presentation on theme: "Socket Programming (Cont.)"— Presentation transcript:

1 Socket Programming (Cont.)
Networking CS 3470, Section 1 Sarah Diesburg

2 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()

3 Byte Ordering High-order byte Low-order byte MSB 16-bit value LSB
Increasing memory address Address A+1 Address A Little-endian byte order High-order byte Low-order byte MSB bit value LSB Consider a 16-bit integer that is made up of 2 bytes: there are two ways to store the 2 bytes in memory, with low order byte at the starting address, known as little endian byte order, or with the high order byte at the starting address, known as big endian byte order Big-endian byte order Low-order byte High-order byte Address A+1 Address A Increasing memory address

4 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

5 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

6 When do we use hton/ntoh functions?
Use hton the port number in struct sockaddr_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

7 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: struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; // IPv4 AF_INET sockets: struct sockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // pad to fit into sockaddr struct in_addr { uint32_t s_addr; // load with inet_pton()

8 Address Conversion Functions
#include <arpa/inet.h> int inet_pton(int af, 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. int inet_aton(const char *strptr, struct in_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

9 Address Conversion Functions
#include <arpa/inet.h> in_addr_t inet_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(struct in_addr inaddr); /* returns: pointer to dotted-decimal string */

10 Example struct sockaddr_in ip4addr; int s; ip4addr.sin_family = AF_INET; ip4addr.sin_port = htons(3490); inet_pton(AF_INET, " ", &ip4addr.sin_addr); s = socket(PF_INET, SOCK_STREAM, 0); bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr);

11 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

12 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 fa13/resources/man_page_levels.htm

13 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>)

14 Byte Manipulation Functions
#include <strings.h> /* Berkeley-derived functions */ void bzero(void *dest, size_t nbytes) Set the first part of an object to null bytes void bcopy(const void *src, void *dest, size_t nbytes); int bcmp(const void *ptr1, const void *ptr2, size_t nbytes) /* return:0 if equal, nonzero if unequal */ #include <string.h> /* ANSI C defined functions */ void *memset(void *dest,int c,size_t len) Sets the first len bytes in memory dest to the value of c void *memcpy(void *dest,const void *src, size_t nbytes) void memcmp(const void *ptr1, const void *ptr2, size_t nbytes)

15 Demo of Program


Download ppt "Socket Programming (Cont.)"

Similar presentations


Ads by Google