DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

gethostbyname(3): What It Does and What to Use Instead

gethostbyname() resolves a host name to a legacy hostent record, but new C code should use getaddrinfo() for modern address-family selection and error handling.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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
Forvencer Server Book, 2 Zipper Pocket, Server Books for Waitress
  • 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 hostent itself 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 with gai_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:

  1. Include <sys/types.h>, <sys/socket.h>, and <netdb.h>.
  2. Set up a struct addrinfo hints 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.
  3. Call getaddrinfo(hostname, service, &hints, &result). For a hostname-only lookup, the service argument can be null.
  4. If the return value is nonzero, report it with gai_strerror(); do not interpret it as h_errno.
  5. On success, iterate through the returned address-info list and use the entries appropriate to your application.
  6. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How 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_DATA or NO_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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.