Computer NetworksNETWORKS

How to get an IP address from a host name? (Example in C)

Patreon ➤ https://www.patreon.com/jacobsorber
Courses ➤ https://jacobsorber.thinkific.com
Website ➤ https://www.jacobsorber.com

How to get an IP address from a host name? // This video shows you how to resolve host names or domain names in your C programs, using the getaddrinfo() function.

Related Videos:
Networks/Network Programming videos: https://www.youtube.com/playlist?list=PL9IEJIKnBJjH_zM5LnovnoaKlXML5qh17

***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I’m a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:
https://www.jacobsorber.com
https://people.cs.clemson.edu/~jsorber/
http://persist.cs.clemson.edu/

To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon — [https://www.patreon.com/jacobsorber]

Source code is also available to Patreon supporters. — [https://jsorber-youtube-source.herokuapp.com/]

source

ipv4

Alice AUSTIN

Alice AUSTIN is studying Cisco Systems Engineering. He has passion with both hardware and software and writes articles and reviews for many IT websites.

16 thoughts on “How to get an IP address from a host name? (Example in C)

  • 11:24 this is the wrong way of getting a name from an addrinfo struct. You should use the getnameinfo(3) function.

    I always thought that using the "old" inet_ntop(3) function was the only way to print an IP address returned by getaddrinfo(3), but it turns out (by reading OpenBSD's inet_ntop(3) man page) that actually the correct and easy way to transform any version of IP address is to use getnameinfo(3) like this:

    char addrstr[INET6_ADDRSTRLEN];
    getnameinfo(addr->ai_addr, addr->ai_addrlen, addrstr, sizeof addrstr, NULL, 0, NI_NUMERICHOST);

    Super easy, right? You just have to know how to do it! As always, we should always read the fantastic manual 😉

  • Maybe for a video you could reimplement traceroute and show how it works. That would be fun and informative. Although, admittedly I have strange ideas of what's fun.

  • In W. Richard Steven's books on tcp/ip, he was saying ntop was network to presentation, and conversely the reverse for pton- Always stuck with me as it's human presentable..

    This was a good video, it's something that usually spins people out a bit. I picked up a whole lot from beej's guide forever ago, like somewhere in the late 90s..

  • Also, one detail that was slightly incorrect in the video, at 3:30.

    "We also have server name."

    While it's outside the scope of this video(which was just about resolving the IP from a domain name), that parameter isn't server name, it's service name. For anyone curious why, you can use this parameter to have getaddrinfo() fill in the required port numbers in the returned structures, as well as the address, family, and protocol info.

    servname takes a string that is either a numeric port number expressed as a string("80"), or a defined service name("http"). It will fill in the corresponding port number in the returned sockaddr structs, so you can pass them straight to connect() or bind().

  • 15:48 "Not sure why it's 2"

    Because UNIX sockets came first 😛

    AF_UNSPEC(unspecfied) is 0, AF_UNIX is 1. AF_INET came next.

  • A Video about UDP would be great. In a lot of books, videos and material are only going in to detail with TCP (bcs. Making your own htttp server and stuff like that).
    So I think it would be great to also learn about UDP.

  • A video on DNS would be great. Also, unrelated, it would also be great if you did a video sometime on message queues, as well. It's the only POSIX IPC that you haven't covered. I figured you'd get to it eventually, but it's been a long time now. So, perhaps it got overlooked? I've noticed a lot of books on IPC overlook message queues as well. Is there a reason for that? Either way, some examples where message queues are a better choice than pipes or shared memory, would be great to see.

  • how about a reverse IP tool? perhaps one that is coded in go language and can work with CMD

  • I came across a very similar program recently in Beej's Guide to Network Programming. Well worth looking up for anyone interested in sockets APIs.

  • In what case will the IP be different for same family(say IPV4)? Why would IP be different for socket and udp? I thought IP layer is agnostic of it. What am I missing?

Comments are closed.