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 7438921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:37:40+00:00 2026-05-29T10:37:40+00:00

Hi I created a function that takes in an accepted sockFD as input and

  • 0

Hi I created a function that takes in an accepted sockFD as input and outputs the ip address in presentation form to a string. The function seems to be working fine until I get down to packing the string with a call from inet_ntop which returns a null pointer and thus giving me my error. The error reads as No space left on device which I don’t understand since I have plenty of ram and rom. Anyways bellow is the function that I am using.

void getTheirIp(int s, char *ipstr){ // int s is the incoming socketFD, ipstr points the the calling
                     // functions pointer.
    socklen_t len;
    struct sockaddr_storage addr;
    len = sizeof(addr);          //I want to store my address in addr which is sockaddr_storage type
    int stat;
    stat = getpeername(s, (struct sockaddr*)&addr, &len); // This stores addrinfo in addr
printf("getTheirIP:the value of getpeername %d\n",stat);
    // deal with both IPv4 and IPv6:
    if ((stat=addr.ss_family) == AF_INET) { // I get the size of the sock first
        printf("getTheirIP:the value of addr.ss_family is %d\n",stat);
        ipstr = malloc(INET_ADDRSTRLEN); // I allocate memory to store the string
        struct sockaddr_in *s = (struct sockaddr_in *)&addr; // I then create the struct sockaddr_in which
                                // is large enough to hold my address
       if(NULL == inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof(ipstr))){ // I then use inet_ntop to
        printf("getTheirIP:the value of inet_ntop is null\n");// retrieve the ip address and store
        perror("The problem was");              // at location ipstr
        }

    } else { // AF_INET6 this is the same as the above except it deals with IPv6 length
        ipstr = malloc(INET6_ADDRSTRLEN);
        struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
        inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof(ipstr));
    }
    printf("%s",ipstr);
}

I left out the rest of the program because it is too big to fit and I only want to focus on fixing this part. However bellow I will show you part of my main() that calls this function.

newSock = accept(listenSock,(struct sockaddr *)&their_addr,&addr_size);
    char *someString;
    getTheirIp(newSock,someString);

Any help would be great. Thanks!

  • 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-29T10:37:40+00:00Added an answer on May 29, 2026 at 10:37 am
    inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof(ipstr))
    

    That sizeof is wrong since ipstr is a pointer (it will yield the size of the pointer, something like 4 or 8). You need to pass the available length of the ipstr buffer.

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

Sidebar

Related Questions

I have created a function that takes a SQL command and produces output that
I recently created a function in javascript that takes in a file name and
I have a function called CreateCriteriaExpression that takes a json string and creates a
I created an EL function that takes no parameters: <function> <name>noArg</name> <function-class>com.example.customtag.function.PrintHi</function-class> <function-signature>java.lang.String noArg()</function-signature>
Is there a way that I can create a function that takes an int
Is it possible to create a template function that takes a variable number of
I've created a javascript function that will take a hidden span, copy the text
I have the following function that takes a number like 5 and creates a
I'm using C structs in objc and I've created a function that assembles the
I've created a javascript function that allows me to validate if one field or

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.