Thread: getservbyname is not working

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    40

    getservbyname is not working

    Code:
    int main(){
        struct servent* se = getservbyname("tejo.ist.utl.pt", "udp");
        printf("%d\n", se->s_port);
        printf("%d\n", ntohs(se->s_port));
    }
    why this code is not working?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,066
    What do you think getservbyname does?
    getservbyname(3)

    It gets a service, not a server.
    The name should be things like "ftp" or "www", not an IP address.

    Your code should also check for NULL pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    40
    ok, what is the diff betwen service and IP?
    service is the port?
    i have seen this page:
    services(5) - Linux manual page
    but i still do not understand it properly

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,066
    Perhaps you should explain what it is you're trying to do, rather than us guess based on your incorrect use of an API.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    40
    here : services(5) - Linux manual page i found this phrase: port number (and protocol) for its service

    and here : getaddrinfo(3) - Linux manual page
    i found the name service which is treated as the port. so i am confused

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    40
    i am trying to better understand the function getservbyname() with some examples

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  2. Getting service port with getservbyname()
    By karas in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 05:16 PM
  3. I really need to get this working =(
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 10-11-2002, 08:35 PM
  4. I really need to get this working =(
    By xlordt in forum C Programming
    Replies: 4
    Last Post: 10-11-2002, 03:51 PM
  5. I really need to get this working =(
    By Unregisterd in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-11-2002, 12:08 PM
Website Security Test