getservbyname is not working

This is a discussion on getservbyname is not working within the Linux Programming forums, part of the Platform Specific Boards category; 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?...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    34
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    34
    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
    34
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21