c-socket-programming:sizeofipv4
این یک نگارش قدیمی از این مطلب است!
سایز IPv4 در حافظه
چون IPv4 یک آدرس 32bit است برای نگهداری آن در حافظه به 4Byte نیاز داریم. با اینکه می توانیم از unsigned int استفاده کنیم ولی درست تر آن است که از struct in_addr استفاده شود که در واقع با unsigned int یکسان است :
// man 7 ip // man 3 inet // Internet address (a structure for historical reasons) typedef uint32_t in_addr_t; struct in_addr { in_addr_t s_addr; // that's a 32-bit int (4 bytes) };
برای نمایش IPv4 احتیاج به یک رشته به طول 16Byte داریم که null terminated است:
xxx.xxx.xxx.xxx'\0'
این مقدار به شکل یک ثابت در INET_ADDRSTRLEN در دسترس است.
نمونه بررسی سایز IPv4 :
#include <stdio.h> #include <arpa/inet.h> int main() { printf("sizeof(struct in_addr) : %d\n", sizeof(struct in_addr)); printf("INET_ADDRSTRLEN : %d\n", INET_ADDRSTRLEN); return 0; }
خروجی :
sizeof(struct in_addr) : 4 INET_ADDRSTRLEN : 16
c-socket-programming/sizeofipv4.1718659580.txt.gz · آخرین ویرایش: 2024/06/18 00:56 توسط pejman
