includesystypes h Datatype Description int 8t uint 8t

  • Slides: 10
Download presentation
#include<sys/types. h> Datatype Description int 8_t uint 8_t int 16_t uint 16_t int 32_t

#include<sys/types. h> Datatype Description int 8_t uint 8_t int 16_t uint 16_t int 32_t uint 32_t Signed 8 -bit integer Unsigned 8 -bit integer Signed 16 -bit integer Unsigned 16 -bit integer Signed 32 -bit integer Unsigned 32 -bit integer sa_family_t socklen_t Address family of socket address structure Length of socket address structure, normally uint 32_t in_addr_t in_port_t IPv 4 address, normally uint 32_t TCP or UDP port, normally uint 16_t

Byte Ordering • 16 -bit integer is made up of 2 bytes. 2 ways

Byte Ordering • 16 -bit integer is made up of 2 bytes. 2 ways to store the bytes in memory • Little-endian byte order: low order byte at the beginning address • Big-endian byte order: high-order byte at the starting address

Network byte order and host byte order • TCP/IP specifies a standard representation for

Network byte order and host byte order • TCP/IP specifies a standard representation for binary integers used in protocol headers called Network byte order that represents integers with the most significant byte first • We refer to the byte ordering used by given system as the host byte order

Byte-ordering functions • Following function converts integers • htons--from host to network byte order

Byte-ordering functions • Following function converts integers • htons--from host to network byte order short unit 16_t htons(unit 16_t value); • ntohs--from network to host byte order short unit 16_t ntohs(unit 16_t value); • htonl—from host to network byte order long unit 32_t htonl(unit 32_t value); • ntohs--from network to host byte order long unit 32_t ntohl(unit 32_t value);

bzero function • Syntax void bzero( void *dest, size_t nbytes); • It is provided

bzero function • Syntax void bzero( void *dest, size_t nbytes); • It is provided by most the system to set nbytes of memory pointed by dest as 0. • Example bzero( &servaddr, sizeof(servaddr)); • Will set servaddr ( server address struct) as 0

Group of address conversion functions int inet_aton( const char *strptr, struct in_addr *addrptr); Returns;

Group of address conversion functions int inet_aton( const char *strptr, struct in_addr *addrptr); Returns; 1 if string was valid, 0 on error convert from ASCII string to network byte order binary values char * inet_ntoa( struct inaddr); Return: pointer to dotted-decimal string Convert from network byte order binary value to dotted decimal string These two functions convert IPv 4 address

 • int inet_pton( int family, const char* strptr, void *addrptr); • Function will

• int inet_pton( int family, const char* strptr, void *addrptr); • Function will convert IP address in string( strptr) to the numeric value as in address struct( addptr). Return 1 if OK, return – 1 on error

 • Const char *inet_ntop(int family, const void *addptr, char *strptr, size_t len); •

• Const char *inet_ntop(int family, const void *addptr, char *strptr, size_t len); • return pointer if K, NULL on error • Does the reverse conversion, from numeric (addptr) to presentation (strptr). The len is the size of the destination, to prevent the function from overflowing the caller’s buffer.

Wrapper functions • In any real world program it is essential to check every

Wrapper functions • In any real world program it is essential to check every function call for error return. Since terminating on an error is the common case, we can shorten our programs by defining a wrapper function that performs the actual function call, tests, the return value, and terminates on an error • s = Socket(AF_INET, SOCK_STREAM, 0);

Wrapper function Socket() int Socket( int family, int type, int protocol) { int n;

Wrapper function Socket() int Socket( int family, int type, int protocol) { int n; if( n = socket( family, type, protocol)) < 0) { printf(“socket errorn”); exit(0); } return n; }