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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:03:21+00:00 2026-05-23T11:03:21+00:00

Here is the function I am trying to get to working on Red Hat

  • 0

Here is the function I am trying to get to working on Red Hat 6..

And I have very little experience with C, and especially using #define, so I’m unsure what this portion is even trying to do: SP->s_port = htons(SP->s_port);

#ifdef __linux
#define GET_SERVICE_BY_NAME(SP, SERVICE, PROTOCOL)                           \
   char            GSBN_servbuf[HOSTBUFFERLENGTH] = {0};                     \
   struct servent  GSBN_sp;                                                  \
   struct servent *GSBN_serv_result;                                         \
   int             GSBN_s = 0;                                               \
   GSBN_s = getservbyname_r(SERVICE,                                         \
                       PROTOCOL,                                             \
                       &GSBN_sp,                                             \
                       GSBN_servbuf,                                         \
                       sizeof(GSBN_servbuf),                                 \
                       &GSBN_serv_result);                                   \
   SP = GSBN_serv_result;                                                    \
   SP->s_port = htons(SP->s_port);                                           \
   if (SP && SOCKET_DEBUG) {                                                 \
      printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", \
             get_timestamp(), SP->s_name, SP->s_port, SP->s_proto);          \
       }                                                                         \
   if (SP == NULL) {                                                         \
      fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n",    \
              get_timestamp(), SERVICE);                                     \
   }
#else
#define GET_SERVICE_BY_NAME(SP, SERVICE, PROTOCOL)                           \
   char            GSBN_servbuf[HOSTBUFFERLENGTH] = {0};                     \
   struct servent  GSBN_serv_result;                                         \
   SP = getservbyname_r(SERVICE,                                             \
                       PROTOCOL,                                             \
                       &GSBN_serv_result,                                    \
                       GSBN_servbuf,                                         \
                       sizeof(GSBN_servbuf));                                \
   if (SP && SOCKET_DEBUG) {                                                 \
      printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", \
             get_timestamp(), SP->s_name, SP->s_port, SP->s_proto);          \
   }                                                                         \
   if (SP == NULL) {                                                         \
      fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n",    \
              get_timestamp(), SERVICE);                                     \
   }
#endif

This is the error I am getting:

According to gdb I am getting a seg fault at this function call:

GET_SERVICE_BY_NAME(sp, serv, prot);

Here is the gdb output:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x456c6c90 (LWP 14645)]
0x420b1e69 in gi_get_port (serv=Unhandled dwarf expression opcode 0x9c
)
    at /home/user1/Common/src/socket.c:282
282           GET_SERVICE_BY_NAME(sp, serv, prot);
Current language:  auto; currently c

Here is how the function is called:

int gi_get_port (char *serv, char *prot)
/* obtain the port for the named service */
{
  int p, s;

  /* Data for resolving service name to a socket description. */
  struct servent *sp = NULL;

  GET_SERVICE_BY_NAME(sp, serv, prot);

  if (sp != NULL) {
    p = sp->s_port;
  } else {
    p = -1;
  };

  return p;
}
  • 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-23T11:03:22+00:00Added an answer on May 23, 2026 at 11:03 am

    This is what your code will look like once the preprocessor is executed :

    int gi_get_port (char *serv, char *prot)
    /* obtain the port for the named service */
    {
      int p, s;
    
      /* Data for resolving service name to a socket description. */
      struct servent *sp = NULL;
    
      char            GSBN_servbuf[HOSTBUFFERLENGTH] = {0};                     
       struct servent  GSBN_sp;                                                  
       struct servent *GSBN_serv_result;                                         
       int             GSBN_s = 0;                                               
       GSBN_s = getservbyname_r(serv,                                         
                           prot,                                             
                           &GSBN_sp,                                             
                           GSBN_servbuf,                                         
                           sizeof(GSBN_servbuf),                                 
                           &GSBN_serv_result);                                   
       sp = GSBN_serv_result;                                                    
       sp->s_port = htons(SP->s_port);                                           
       if (sp && SOCKET_DEBUG) {                                                 
          printf("%s GET_SERVICE_BY_NAME - Service: %s Port: %d Protocol: %s\n", 
                 get_timestamp(), sp->s_name, sp->s_port, sp->s_proto);          
           }                                                                         
       if (sp == NULL) {                                                         
          fprintf(stderr, "%s GET_SERVICE_BY_NAME - Service %s not found.\n",    
                  get_timestamp(), serv);                                     
       }
    
      if (sp != NULL) {
        p = sp->s_port;
      } else {
        p = -1;
      };
    
      return p;
    }
    

    As you see, you should be checking for ‘sp’ being ‘NULL’ before you do the htons() on the port.

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

Sidebar

Related Questions

We have been wrestling with the confines of ASP.NET trying to get JSONP working.
I've been trying to get this Excel function working correctly, and I've hit a
I'm trying to get my page listing function working in ASP with an Access
I'm trying to get sockets working with Chrome 16 which is using the latest
Update: I did get this working, here is the new javascript: $(document).ready(function() { $('a').click(
Here is what I'm trying to do: - i need a function that when
I'm trying to parallelize a convolution function in C. Here's the original function which
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
Is it possible to have default arguments in MATLAB? For instance, here: function wave(a,
Someone posted a great little function here the other day that separated the full

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.