Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice.

Similar presentations


Presentation on theme: "CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice."— Presentation transcript:

1 CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice versa Lecture 191CS311 - Operating Systems I

2 Internet Sockets Processes communicate via network. Two identify a process on the network we need – IP address of the process (IPv4 or IPv6) – Port number where the process listens Lecture 192CS311 - Operating Systems I

3 Getting the IP Address To get the IP address in network byte order for the IP that is known use inet_addr(). Prototype: in_addr_t inet_addr(const char *string) – Returns the 32-bit IP of network byte order corresponding to the A.B.C.D string. If IP not known then we can get that using gethostname() and gethostbyname() Lecture 193CS311 - Operating Systems I

4 gethostname() and gethostbyname() gethostname() returns the host’s name where the process is executing. Prototype: int gethostname(char *name, int namelen) gethostbyname() returns a pointer to struct hostent Protoype: struct hostent * gethostbyname(const char *name) – The only parameter is the hostname. – Searches the /etc/host or the DNS database to get the structure information. Lecture 194CS311 - Operating Systems I

5 struct hostent The structure has these members – char *h_name - “official” name of the host. – char **h_aliases - alternative names for the host – int h_addrtype - either AF_INET or AF_INET6 – int h_length - length, in bytes, of each address. – char **h_addr_list – array of addresses for the host. – char *h_addr - first host address (or *h_addr_list[0]). Lecture 195CS311 - Operating Systems I

6 inet_ntoa() and inet_aton() inet_ntoa() - Takes IP address in network-byte order and converts to string format. Prototype: char *inet_ntoa(struct in_addr addr) inet_aton() – same as inet_addr() but better Prototype: int inet_aton(const char *addr, struct in_addr *addr) – Stores the char* addr into struct in_addr Lecture 196CS311 - Operating Systems I

7 memset() and others memset() sets a block of memory to the desired value. Prototype: void * memset (void * ptr, int value, size_t num); memcpy() copies a block of memory to another block. Prototype: void * memcpy ( void * destination, const void * source, size_t num ); memcmp() compares two blocks of memory Prototype: int memcmp ( const void * ptr1, const void * ptr2, size_t num ); Lecture 197CS311 - Operating Systems I

8 bzero() and bcopy() bzero() same as memset() but initializes a block of memory to zero. Prototype: void bzero (void* buffer, size_t length) bcopy() is the same as memcpy() Lecture 198CS311 - Operating Systems I

9 Machine-byte to network-byte Converting machine-byte order to network order Protoype: – in_port_t htons (in_port_t hostShort) – in_addr_t ntohl (in_addr_t networkLong) Converting network-byte to machine-byte Prototype: – in_port_t ntohs (in_port_t networkShort) – in_addr_t htonl (in_addr_t hostLong) Lecture 199CS311 - Operating Systems I


Download ppt "CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice."

Similar presentations


Ads by Google