Fill This Form To Receive Instant Help
Homework answers / question archive / The system function with the prototype: uint16_t ntohs (uint16_t net16bitvalue) can be used to convert a 16-bit port number from the network -byte order to a host -byte order
The system function with the prototype: uint16_t ntohs (uint16_t net16bitvalue) can be used to convert a 16-bit port number from the network -byte order to a host -byte order. Write a C function to implement the ntohs function as shown in the following program, where the utility function byte_order returns the logical value true if the system has the same byte ordering as the Internet protocols;otherwise, it returns false.
typedef union { uint16_t s; char c [2]; } UN;
bool byte_order ()
{
UN un; un.s = 0x0102;
return (un.c [0] == 1 && un.c [1] == 2) ? true : false;
}
uint16_t ntohs (uint16_t net16bitvalue)
{
// FILL IN YOUR STATEMENTS HERE
}
int main ()
{
uint16_t port_num;
printf (“ enter the port number in network -byte order: ”);
scanf (“%hd”, & port_num);
printf (“ port number in host -byte order = %d \n”, ntohs (port_num));
exit (0);
}