mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-04-05 19:57:44 +08:00
3.4完成
This commit is contained in:
22
ch03/inet_ntoa.c
Normal file
22
ch03/inet_ntoa.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct sockaddr_in addr1, addr2;
|
||||
char *str_ptr;
|
||||
char str_arr[20];
|
||||
|
||||
addr1.sin_addr.s_addr = htonl(0x1020304);
|
||||
addr2.sin_addr.s_addr = htonl(0x1010101);
|
||||
//把addr1中的结构体信息转换为字符串的IP地址形式
|
||||
str_ptr = inet_ntoa(addr1.sin_addr);
|
||||
strcpy(str_arr, str_ptr);
|
||||
printf("Dotted-Decimal notation1: %s \n", str_ptr);
|
||||
|
||||
inet_ntoa(addr2.sin_addr);
|
||||
printf("Dotted-Decimal notation2: %s \n", str_ptr);
|
||||
printf("Dotted-Decimal notation3: %s \n", str_arr);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user