Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7405295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:22:57+00:00 2026-05-29T05:22:57+00:00

I am writing a basic UDP Client-Server program and wasn’t getting the expected results

  • 0

I am writing a basic UDP Client-Server program and wasn’t getting the expected results from getbyhostname(). Here is a snippet from my code:

char *clientHostName = malloc(HOST_NAME_MAX);
gethostname(clientHostName, HOST_NAME_MAX);
printf("%s\n",clientHostName);
struct hostent thehost = gethostbyname(clientHostName);
printf("%ld\n",(*((unsigned long *) thehost->h_addr_list[0])));

So, the first print statement outputs what I expected, the name of my computer. However, I expect the second print statement to print out my IP Address. But no, it print out something like this: 4398250634. What is this that it is printing out? How do I get my IP Address?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:22:58+00:00Added an answer on May 29, 2026 at 5:22 am

    First of all, you should not be using the gethostbyname interface. It’s deprecated and cannot deal with IPv6, which is a real-world, practical show-stopper in 2012. The proper interface to use is getaddrinfo. Once you’ve used getaddrinfo to lookup a hostname and have it in a socket address form, you can use getnameinfo with the NI_NUMERICHOST flag to convert it to a printable IP-address form. This works for either IPv4 or IPv6, or for any future protocols.

    As for your particular printing issue, how do you expect %ld to print an IP address? It prints a single number (long) in decimal (base 10). You could instead cast the pointer to unsigned char * and read 4 elements, each to be printed with %d, but again this is a bad approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a basic Server-client program in Java and I'm trying to handle the
I am implementing a really basic server-client model in Java, by using UDP sockets
I'm writing a basic program that copies a string from an existing text file
I am writing a basic http server in C. Handling a simple static .html
When writing a basic c program. #include <stdio.h> main(){ printf(program); } Is the definition
I'm writing a basic UNIX program that involves processes sending messages to each other.
I am writing a basic timer program with Java, and in the program I
I'm writing a basic client to access the Amazon SimpleDB service and I'm having
I am writing a basic windows program and I am looking for some basic
I'm writing a basic java card game but I'm getting a java.lang.IndexOutOfBoundsException: Index: 6,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.