2

I'm studying C and TCP/UDP. As the title... Is there any difference between inet_aton() and gethostbyname() in C? From what I know, both convert an IP address from a string to a number.

testermaster
  • 1,031
  • 6
  • 21
  • 40

2 Answers2

5

gethostbyname() is obsolete. You should use getaddrinfo().

inet_aton() only works for IPv4.

Also, inet_aton() only convert a IPv4 notion (0.0.0.0) to int, getaddrinfo does DNS resolution.

blue112
  • 52,634
  • 3
  • 45
  • 54
  • 2
    Plus there's the fact that `getaddrinfo` (and the old `gethostbyname`) can also look up hostnames via DNS, while `inet_aton` only works with IP addresses in string form. – Matti Virkkunen Apr 15 '14 at 15:15
2

gethostbyname() is used for getting ip-address from hostname and store them in struct in_addr.Where as inet_aton takes in the ip address in dotted format and converts into network byte order .

Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49