getservbyport()--Get Service Name for Port Number


  BSD 4.3 Syntax
  #include <netdb.h>

 struct servent *getservbyport(int port_number,
                              char *protocol_name)

  Service Program Name: QSOSRV2

  Default Public Authority: *USE

  Threadsafe: No; see Usage Notes.



  UNIX 98 Compatible Syntax
  #define _XOPEN_SOURCE 520
  #include <netdb.h>

 struct servent *getservbyport(int port_number,
                              const char *protocol_name)

  Service Program Name: QSOSRV2

  Default Public Authority: *USE

  Threadsafe: No; see Usage Notes.


The getservbyport() function is used to retrieve information about a service assigned to a port number. The information is retrieved from the service database file.

There are two versions of the API, as shown above. The base i5/OS API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.


Parameters

port_number
(Input) The port number for which service information is to be retrieved.

protocol_name
(Input) The pointer to the character string that contains the name of the protocol that further qualifies the search criteria. For example, if the port_number is 10, and the protocol_name is tcp, then the call will return the server that uses the TCP protocol on port number 10. If this parameter is set to NULL, then the first server is returned, regardless of the protocol used.

Authorities

No authorization is required.


Return Value

getservbyport() returns a pointer. Possible values are:

The structure struct servent is defined in <netdb.h>.

      struct servent {
        char            *s_name;
        char            **s_aliases;
        int             s_port;
        char            *s_proto
      };

s_name points to the character string that contains the name of the service. s_aliases is a pointer to a NULL-terminated array of alternate names for the service. s_port is the port number assigned to the service. s_proto is the protocol being used by the service.


Usage Notes

  1. The iSeries Navigator or the following CL commands can be used to access the services database file:


  2. The pointer returned by getservbyport() points to static storage that is overwritten on subsequent calls to the getservbyport(), getservbyname(), or getservent() functions.

  3. When the service information is obtained from the service database file, the file is opened and the service information is retrieved (if it exists) from the file. The file is then closed only if a setservent() with a nonzero parameter value was not previously done.

  4. A coded character set identifier (CCSID) of 65535 for the job requests that no database translation be performed. For translation to occur for the protocol name specified by the protocol_name parameter, or for the service names returned in the servent structure, the job CCSID must be something other than 65535.

  5. Do not use the getservbyport() function in a multithreaded environment. See the multithread alternative getservbyport_r() function.

  6. When you develop in C-based languages and an application is compiled with the _XOPEN_SOURCE macro defined to the value 520 or greater, the getservbyport() API is mapped to qso_getservbyport98().

Related Information



API introduced: V4R2
Top | UNIX-Type APIs | APIs by category