get IP address of a given machine
2013-11-18 09:50
381 查看
getaddrinfo在linux man page中的详细解释:
http://linux.die.net/man/3/getaddrinfo
/* homework1_getaddr:get the IP addresses of a given machine */
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
struct addrinfo hints,*result,*a;
struct timeval start,end;
struct hostent *hptr;
int main(int argc,char *argv[])
{
//to figure out if the hostname is inserted correctly
if(argc!=2)
{
printf("Please insert the hostname.\n");
return 1;
}
//memory set and define values for hints
bzero(&hints,sizeof(hints));
hints.ai_family=AF_INET;
hints.ai_socktype=SOCK_STREAM;
//to figure out if the hostname is valid
int addrinforeturn=0;
gettimeofday(&start,NULL);
if (addrinforeturn!=getaddrinfo(argv[1], NULL, &hints, &result))
{
printf("Please check if you insert correctly\n");
return 2;
}
gettimeofday(&end,NULL);
//to print the execution time
unsigned long usedtime=1000*(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec)/1000;
printf("execution time:%u milliseconds\n",usedtime);
//to print the official name from the gethostbyname function
hptr=gethostbyname(argv[1]);
printf("official name:%s \n",hptr->h_name);
//to print the aliases if it really has
char **aptr;
for(aptr=hptr->h_aliases;*aptr!=NULL;aptr++)
printf("Aliases:%s\n",*aptr);
//to convert the IP to a string and print the addresee
for (a=result; a!=NULL; a=a->ai_next)
{
void *addr;
char ipaddr[INET_ADDRSTRLEN];//address string length for ip
struct sockaddr_in *ip=(struct sockaddr_in *)a->ai_addr;//transfer the struct type from scokaddr to scokaddr_in
addr=&(ip->sin_addr);
inet_ntop(AF_INET, addr, ipaddr, sizeof(ipaddr));//convert the type of the address
printf("%s\n",ipaddr);
}
//clear memory for the linked list
freeaddrinfo(result);
return 0;
}
http://linux.die.net/man/3/getaddrinfo
/* homework1_getaddr:get the IP addresses of a given machine */
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
struct addrinfo hints,*result,*a;
struct timeval start,end;
struct hostent *hptr;
int main(int argc,char *argv[])
{
//to figure out if the hostname is inserted correctly
if(argc!=2)
{
printf("Please insert the hostname.\n");
return 1;
}
//memory set and define values for hints
bzero(&hints,sizeof(hints));
hints.ai_family=AF_INET;
hints.ai_socktype=SOCK_STREAM;
//to figure out if the hostname is valid
int addrinforeturn=0;
gettimeofday(&start,NULL);
if (addrinforeturn!=getaddrinfo(argv[1], NULL, &hints, &result))
{
printf("Please check if you insert correctly\n");
return 2;
}
gettimeofday(&end,NULL);
//to print the execution time
unsigned long usedtime=1000*(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec)/1000;
printf("execution time:%u milliseconds\n",usedtime);
//to print the official name from the gethostbyname function
hptr=gethostbyname(argv[1]);
printf("official name:%s \n",hptr->h_name);
//to print the aliases if it really has
char **aptr;
for(aptr=hptr->h_aliases;*aptr!=NULL;aptr++)
printf("Aliases:%s\n",*aptr);
//to convert the IP to a string and print the addresee
for (a=result; a!=NULL; a=a->ai_next)
{
void *addr;
char ipaddr[INET_ADDRSTRLEN];//address string length for ip
struct sockaddr_in *ip=(struct sockaddr_in *)a->ai_addr;//transfer the struct type from scokaddr to scokaddr_in
addr=&(ip->sin_addr);
inet_ntop(AF_INET, addr, ipaddr, sizeof(ipaddr));//convert the type of the address
printf("%s\n",ipaddr);
}
//clear memory for the linked list
freeaddrinfo(result);
return 0;
}
相关文章推荐
- How to get local machine name and IP address?
- Get IP address of the user in SAP ABAP
- Using PowerCLI to get the IP address of a VM
- Get IP address of iPhone
- Android: get IP address of device
- Get the IP address of a network interface in C using SIOCGIFADDR
- Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法输入日志标题
- Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法
- note : get address of KiFastCallEntry
- Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hos
- Correct Method of Changing the IP Address in 11i & R12 (Doc ID 751328.1)
- get register by address instead of name
- Changing the IP Address of a Domain Controller
- Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法
- Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法
- Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hos
- Get machine IP and location via open api private static string GetOwnPublicIP() {
- Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法
- [INS-06101] IP address of localhost could not be determined 解决方法
- InetAddress.getLocalHost().getHostAddress()获取的ip为127.0.0.1