gethostbyname() is a legacy C library function that resolves a host name to a struct hostent. It is IPv4-oriented and obsolete; for new forward-lookup code, use getaddrinfo(), which supports modern address-family selection and has a clearer error interface.
What does gethostbyname() do?
Declared in <netdb.h>, gethostbyname() takes a host name and returns a pointer to a struct hostent containing the resolved host information. Resolution follows the system’s configured host-resolution sources; on Linux these can include DNS or other name services, /etc/hosts, and NIS/YP, depending on resolver configuration such as /etc/nsswitch.conf. See the Linux gethostbyname(3) manual.
| # | Preview | Product | Price | |
|---|---|---|---|---|
| 1 |
|
Forvencer Server Book, 2 Zipper Pocket, Server Books for Waitress | $7.99 | Buy on Amazon |
| 2 |
|
DNS and BIND (5th Edition) | $38.88 | Buy on Amazon |
| 3 |
|
Domain Name Server (DNS) Fundamentals: Exploring Traceroute, DNS Attacks and Beyond | $14.99 | Buy on Amazon |
As an Amazon Associate I earn from qualifying purchases.
The returned structure contains:
h_name: the host’s official name.h_aliases: a list of aliases.h_addrtype: the address family.h_length: the length of an address.h_addr_list: a list of addresses.
The LSB interface description allows a host name or numeric address as input. For an IPv4 address in dotted-decimal form, the documented behavior is to return that address in the host entry without performing a name lookup. See the LSB gethostbyname(3) specification.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Why is gethostbyname() obsolete?
The Linux man-pages Library Functions Manual (2026 edition) states: “The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() functions are obsolete.” The history explains the change: POSIX.1-2001 marked gethostbyname(), gethostbyaddr(), and h_errno obsolescent; POSIX.1-2008 removed their specifications and recommended modern alternatives. The current status is documented in the Linux manual page.
#1 Best Overall
- Upgraded Two Zipper Pockets: Forvencer server books feature two secure zipper pockets for better organization of coins, cash, and receipts, ensuring that everything you collect has a safe and secure place
- Smart Storage & Quick Access: Designed with 8 multi-functional compartments, the right side includes a guest receipt pad, while the left has a money pocket, ticket pocket, and credit card slot. Two small clear pockets store bills, receipts, and other visible items. A stitched pen loop ensures you always have your favorite pen ready
- High-quality & Easy to Clean: Crafted from high-quality PU leather with heavy-duty stitching, this server book is built to last. It resists tears, scratches, and its waterproof surface makes cleaning easy with just a damp cloth or a non-chlorine sanitizer
- Perfect Fit for Your Apron: Measuring 5” x 8”, this compact organizer is slightly smaller than other models, making it ideal for bending or sitting while carrying in your server apron. It holds everything a waitress needs—a place for everything
- What's Included: This server organizer comes with multiple open and zippered pockets to store money, receipts, tips, etc. Clear sleeves are perfect for keeping menus or special lists while serving. Available in a variety of colors, allowing you to express yourself even when in uniform
There are practical reasons to migrate as well:
- Address-family limits: the legacy interface is IPv4-oriented. It does not provide the family selection needed for code that must handle IPv4 and IPv6 through one lookup interface.
- Shared result storage: legacy non-reentrant calls can return pointers to static storage that a later call overwrites. Copying only the
struct hostentitself is not enough because its fields point to other data. - Older error reporting: failures are indicated by a null pointer and explained through
h_errno, rather than by a function return code paired withgai_strerror().
What should replace it?
For a forward lookup in new C code, use getaddrinfo(). It supports explicit address-family selection and returns results through the modern address-info interface. Check its return value and use gai_strerror() to turn a nonzero error code into a diagnostic. The Linux manual recommends getaddrinfo(3) for forward lookups and getnameinfo(3) for reverse lookups or presenting an address as a name. See gethostbyname(3) and getaddrinfo(3).
| Need | Legacy interface | Modern interface |
|---|---|---|
| Forward lookup | gethostbyname(), IPv4-oriented |
getaddrinfo(), with address-family selection |
| Reverse lookup or name presentation | gethostbyaddr() |
getnameinfo() |
| Error reporting | Null pointer; inspect h_errno; legacy helpers include herror() and hstrerror() |
Check getaddrinfo()’s return code and format it with gai_strerror() |
| Result storage | Non-reentrant forms may use static storage overwritten by later calls | Uses the modern address-info result interface; consult the API documentation for allocation and release requirements |
| Resolver configuration | Uses configured host-resolution sources | Uses the system resolver interface; consult getaddrinfo(3) for its behavior and options |
How do you resolve a hostname in C?
A minimal forward-lookup pattern is to call getaddrinfo(), inspect its return value, process the returned address information, and release the result using the API’s documented cleanup function. This outline avoids assuming a particular socket type or address-family policy:
Rank #2
- Include
<sys/types.h>,<sys/socket.h>, and<netdb.h>. - Set up a
struct addrinfohints value for the address families and socket type your application needs; use an unspecified family when it should accept the system’s available IPv4 or IPv6 results. - Call
getaddrinfo(hostname, service, &hints, &result). For a hostname-only lookup, the service argument can be null. - If the return value is nonzero, report it with
gai_strerror(); do not interpret it ash_errno. - On success, iterate through the returned address-info list and use the entries appropriate to your application.
- Release the list with
freeaddrinfo()when finished.
Exact flags and hints depend on whether the program needs a numeric address, a particular socket type, or a specific address family. The getaddrinfo(3) documentation defines those options and the result lifecycle.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteHow did legacy lookup errors work?
gethostbyname() reports failure by returning a null pointer. The associated h_errno value distinguishes several resolver failure classes:
HOST_NOT_FOUND: the host is unknown.NO_DATAorNO_ADDRESS: the name is valid but has no address.NO_RECOVERY: a nonrecoverable resolver failure occurred.TRY_AGAIN: a temporary failure occurred at an authoritative server.
These are legacy diagnostics. In migrated code, handle the error code returned by getaddrinfo() and format it with gai_strerror(), as described in the legacy manual page and getaddrinfo(3).
When might existing code still use it?
Older code may retain gethostbyname() for compatibility, especially where its IPv4-only behavior is intentional and the program’s storage and threading constraints are understood. It is not the recommended choice for new code, and changing the function call alone is not always a complete migration: callers must also adapt to the new result structure, error handling, address-family behavior, and result cleanup.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.




