Part 1: 实验目的掌握sockaddr_in 应用的三个步骤,并能熟练运用过程中相关函数。 Part2:实验内容
1 1 #include<stdio.h> 2 2 #include<stdlib.h> 3 3 #include<string.h> 4 4 #include<unistd.h> 5 5 #include<sys/types.h> 6 6 #include<sys/socket.h> 7 7 #include<netinet/in.h> 8 8 #include<arpa/inet.h> 9 9 #define PORT 1234 10 10 main() 11 11 { 12 12 int i; 13 13 struct sockaddr_in server; 14 14 bzero(&server,sizeof(server)); 15 15 printf("Your server‘s ip init mem is \n"); 16 16 for(i=0;i<sizeof(server);i++) 17 17 { 18 18 printf(" %02x ",*((unsigned char*)(&server)+i)); 19 19 } 20 20 printf("\n"); 21 21 printf("\n"); 22 22 server.sin_family=AF_INET; 23 23 server.sin_port=htons(PORT); 24 24 server.sin_addr.s_addr=inet_addr("192.168.0.21"); 25 25 printf("Your server‘s ip sin_family is %d\n",server.sin_family); 26 26 printf("Your server‘s ip is %s, port is %d\n",inet_ntoa(server.sin_addr),ntohs(server.sin_port)); 27 27 printf("Your server‘s length is %d\n",sizeof(server)); 28 28 printf("Your server‘s ip mem is \n"); 29 29 for(i=0;i<sizeof(server);i++) 30 30 { 31 31 printf(" %02x ",*((unsigned char*)(&server)+i)); 32 32 } 33 33 printf("\n"); 34 34 return; 35 35 }
时间: 2024-10-13 05:52:39