Visit Our UNIX and Linux User Community


getservbyport - Always returning NULL servent


 
Thread Tools Search this Thread
Top Forums Programming getservbyport - Always returning NULL servent
# 1  
Old 12-31-2008
getservbyport - Always returning NULL servent

Hi,

I am having an issue using getservbyport. Here is a little program to demonstrate the problem (removed the includes):
Code:
int 
main(void) {

        struct servent *service;
        int memsize = sizeof(struct servent);

        service = (struct servent *)malloc(memsize);
        bzero(service, memsize);
        service = getservbyport(25, NULL);
        if (service == NULL)
        {
                perror("getservbyport()");
                exit(1);
        }
        printf("service name = %s\n", service->s_name);

        return 0;
}

Here is how I am compiling and running this program
Code:
me@host:~$ gcc -o foo foo.c 
me@host:~$ ./foo
getservbyport(): Success

I was expecting to see "service name = smtp", since my /etc/services file contains the following two lines:
Code:
smtp             25/tcp    mail         #Simple Mail Transfer
smtp             25/udp    mail         #Simple Mail Transfer

I tried this on two hosts with the same result. One has gcc 4.1.2 and the other has gcc 2.96. I also get the same result, on both hosts, if I specify the protocol to use when calling getservbyport (instead of NULL which matches any protocol).

Can anyone see what I am doing wrong, or why a NULL result from the call to getservbyport would say "Success" from the perror call? The man page for getservbyport says the following:
Code:
       The getservent(), getservbyname() and getservbyport() functions  return
       the  servent structure, or a NULL pointer if an error occurs or the end
       of the file is reached.

It could be reaching EOF, but again, the two lines are present for smtp in my /etc/services file.

Thanks,
goon12
# 2  
Old 12-31-2008
You're getting a bit tied up with pointer handling, but line 5 is where the real change is.

c code:
  1. int
  2. main(void) {
  3.  
  4.         struct servent *service;
  5.         service = getservbyport(htons(25), NULL);
  6.         if (service == NULL)
  7.         {
  8.                 perror("getservbyport()");
  9.                 exit(1);
  10.         }
  11.         printf("service name = %s\n", service->s_name);
  12.  
  13.         return 0;
  14. }
# 3  
Old 12-31-2008
Thanks reborg, I missed that in the man page.
Code:
       The getservbyport() function returns a servent structure for the line that  matches
       the  port  port given in network byte order using protocol proto. If proto is NULL,
       any protocol will be matched.

Thanks again,
Joe

Previous Thread | Next Thread
Test Your Knowledge in Computers #790
Difficulty: Easy
The AND gate is a basic digital logic gate that implements logical conjunction.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Malloc function returning NULL

Hi All, I am using malloc function for allocating dynamic memory. When I am using below code on Linux server its working fine, but When I am trying the same code on HP UNIX server its returning NULL. below is a fragment of code in which it is giving problem. tmp = (format_tree... (4 Replies)
Discussion started by: Taher Saifuddin
4 Replies

2. Programming

Getpwnam_r returning null with errno 25

I am calling getpwnam_r with all proper argument as below:- rv = getpwnam_r(name, result, buffer, buflen); This program runs fine on sol 8/9/10. But on sol 11 it returns NULL with errno set to 25 (#define ENOTTY 25 /* Inappropriate ioctl for device */) All boxes are... (2 Replies)
Discussion started by: Ranajit
2 Replies

3. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

4. UNIX for Advanced & Expert Users

getservbyname returning NULL

OS : Solaris 10 When I try to get the "echo" service port, getservbyname is returning null. I checked - /etc/services having an entry for echo - echo 7/tcp (But still getservbyname returning null) Any other config required to consider? (1 Reply)
Discussion started by: satish@123
1 Replies

5. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

6. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

7. Shell Programming and Scripting

Script returning null results

Hi, The following shell script returning null results could you please tell me whats the problem in script, ********************************* #!/bin/ksh . $HOME/conf/systemProperties/EnvSetup.properties a=`date +"%y%m%d"` set -x for i in `cat... (2 Replies)
Discussion started by: shivanete
2 Replies

8. UNIX for Dummies Questions & Answers

malloc returning NULL if freemem high & swapmem low (MPRAS version 3.03 )

Hi All,:) In my application malloc is returning NULL even though there is sufficient amount of free memory available but the swap memory is low. Is this possible that, if free memory is high & swap memory is low, malloc will not be able to allocate memory & return NULL ? Few details: ... (4 Replies)
Discussion started by: Ritesh Kumar
4 Replies

9. Solaris

malloc returning NULL if freemem high & swapmem low

Hi All, In my application malloc is returning NULL even though there is sufficient amount of free memory is available but swap memory is low. Is this possible that, if free memory is high & swap memory is low, malloc will not be able to allocate memory & return NULL ?:) Kindly look into... (5 Replies)
Discussion started by: Ritesh Kumar
5 Replies

10. Shell Programming and Scripting

compare null with non-null

I've got a very peculiar situation. I'm trying to find out if we can compare null fields with non-null. I've output csv files from SQL and Oracle. I need to compare each field from the files, and then find out any differences. The files usualy have over 500 fields, and send the resule to DBA.... (8 Replies)
Discussion started by: nitin
8 Replies

Featured Tech Videos