It is important to note that different machines, some of which may be big endian bytes, and some may be small byte-order.
The small end is the low byte emissions at the lower address of the memory is the starting address of the value, high-bit bytes emitted in the memory of the higher address.
The big end is that the high byte is emitted at the low address of the memory that is the starting address of the value, and the low byte is emitted at the high address end of memory.
The implementation code is as follows:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <sys/socket.h>5#include <netinet/inch.h>6#include <arpa/inet.h>7 #defineIP "180.97.33.107"8 intMy_aton (Char*IP)9 {Ten intarr[4]; One inti; ASSCANF (IP,"%d.%d.%d.%d", arr,arr+1, arr+2, arr+3); -I= (arr[0]<< -)| (arr[1]<< -)| (arr[2]<<8) |arr[3]; - returni; the } - Char* MY_NTOA (inti) - { - Static Charbuf[1024x768]; +memset (BUF,0,1024x768); -sprintf (BUF,"%d.%d.%d.%d", (i>> -) &0xFF, (i>> -) &0xFF, (i>>8) &0xFF,i&0xFF); + returnbuf; A } at intMainintargcChar*argv[]) - { - inti; -I=My_aton (IP); - structin_addr My_add; -Inet_aton (ip,&my_add); inprintf"%x\n", i); -printf"%x\n", my_add.s_addr); to Char*buf; +buf=My_ntoa (i); -printf"%s\n", buf); theprintf"%s\n", Inet_ntoa (My_add)); *}View Code
Get the local IP method as follows:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<netdb.h>intMainintargcChar*argv[]) { structHostent *p; P=gethostbyname (argv[1]); Char**pp; printf ("Name:%s\n",p->h_name); PP=p->h_aliases; for(; *pp!=null;*pp++) printf ("Alia:%s\n",*PP); printf ("Type:%d\n",p->H_addrtype); printf ("IP Length:%d\n",p->h_length); PP=p->h_addr_list; for(; *PP! = null;*pp++) printf ("IP:%s\n", Inet_ntoa (* (structin_addr*) *pp)); return 0;}View Code
Linux Basics-View the simple implementation of IP and port