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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:56:32+00:00 2026-06-05T08:56:32+00:00

I need to do simple client program, when I add port number and client

  • 0

I need to do simple client program, when I add port number and client will be serch all services for this port. Now is problem with segmentation fault in if statement.
How to return all services? In my program it will be return just one, I think.

my code:

   int main (int argc, char *argv[])
{
    int sockfd, n,pol, s;
    int numer;
    char recvline[MAXLINE +1];
    char  p;
    struct sockaddr_in servaddr;
    struct servent *sp;

    if (argc != 3)
        err_sys("Aby uruchomić podaj: klient <Adres IP> <port>");

    s = atoi(argv[2]);
    if((sp = getservbyport(s,NULL)) == NULL)
        {
            printf("port (s): %d \n", s);
            printf("port (sp): %d \n", sp->s_port); //segmentation fault
            err_sys("problem with port");   
        }

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
        err_sys("Blad utworzenia polaczenia");
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = sp->s_port;
    if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr)<=0)
        err_sys_kom("Blad konwersji do adresu IP dla %s", argv[1]);

    printf("%s", sp->s_name);
    pol = connect(sockfd, (SA*) &servaddr, sizeof(servaddr));
    if (pol < 0)
       {
          err_sys_kom("Blad polaczenie z serwerem");
          close(sockfd);
          exit(-1);
       }
    else
        str_cli(stdin, sockfd , 1);

exit(0);
}
  • 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-06-05T08:56:34+00:00Added an answer on June 5, 2026 at 8:56 am

    EDIT (in response to new new problem – see comments below):

    You probably need to get a list of protocols and work with those in a loop. The contents of the loop should roughly be:

    1. Call getprotoent
    2. If the result is NULL, exit the loop.
    3. Else, dig out the protocol name from the returned structure.
    4. Use that name as the second argument for getservbyport
    5. Do what you want to with the result

    EDIT (in response to new problem):

    if((sp = getservbyport(s,NULL)) == NULL)
    

    So your logic is to read sp ONLY IF sp is NULL. Obviously it will segfault.

    It should be:

    if((sp = getservbyport(s,NULL)) != NULL)
    

    But then you will point out another new problem:

    Why is sp NULL?

    This could be because (as per the earlier version of my answer), you did an atoi on something which was not an integer. It could be because of any other reason. We can’t say because we don’t know what input you give.


    This following part of the answer was in response to an old problem that the OP asked in the same question, and has since then chosen to edit over it:

    First of all: Since you’re using getservbyport, you really should read about services, if you haven’t already.

    Now on to the error:

    getservbyport is of type:

    struct servent *getservbyport(int port, const char *proto);
    

    You are passing argv[2] which is of type char * instead of an int for port.

    I believe the user inputs this as an argument in your program?

    If you know that a char * points to a set of characters which look like an integer, like “1024”, then you can convert it to an integer with atoi.

    Do this instead, on the line with the error, when calling getservbyport, while making sure you’ve included stdlib.h:

    getservbyport(atoi(argv[2]),NULL)
    

    If argv[2] is NOT representable as an integer, you’ll get undefined behavior, so maybe, you’ll want to check this first.

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

Sidebar

Related Questions

I have assignment when I need to write simple time server and a client
i am writing a simple client/server chat program with indy 10 (blocking mode) and
I have a simple UDP socket program in C. The client transmits data to
I am engaging the following problems: I need to write a server program, will
I have written a simple client server program in C under linux. I have
Are there any decent BitTorrent libraries for Java? I need to program a simple
How do I create unique key value in PHP? I need simple unique keys
I need a simple jQuery photo gallery (just a table of images) which supports
I need a simple way without any Custom listAdapters to create a ListView with
I need a simple help: need to match: [sometext] [sometext value=1] [sometext value=1 anothervalue=2]

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.