Thread: Getting service port with getservbyname()

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78

    Getting service port with getservbyname()

    Hi,
    the following code
    Code:
    int main()
    {
    	struct servent* se = getservbyname("telnet", "tcp");
    	printf("%d\n", se->s_port);
    }
    prints as port number 5888 but in /etc/services this protocol has default port 23. And generally, getting port value with this function for any service differs from the values written in /etc/services. Why is that happening?
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    try converting it to host byte order.
    Code:
    printf("%d\n", ntohs(se->s_port));

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    It works, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  2. running my program as service
    By daher in forum Windows Programming
    Replies: 5
    Last Post: 09-05-2008, 12:30 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 03:28 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM