---->getservbyport()---->/etc/services

    1  /*
    2   * Probe service name by port number
    3   * cc getserv.c -lnsl -o getserv
    4   */
    5  #include <stdio.h>
    6  #include <errno.h>
    7  #include <netdb.h>
    8  #include <stdlib.h>
    9
   10  int
   11  main(int argc, char *argv[])
   12  {
   13      struct servent *serv ;
   14      int portnum;
   15
   16      if(argc != 2){
   17          printf("Usage: %s port\n", argv[0]);
   18          exit(1);
   19      }
   20
   21      portnum = atoi(argv[1]);
   22      if ( ( serv = getservbyport(htons(portnum),"tcp")) == NULL ){
   23          perror("getservbyport:");
   24          exit(1);
   25      }
   26      printf( "port %d is %s\n", portnum , serv->s_name );
   27      exit(0);
   28  }

getserv.c

% ./getserv 25
port 25 is smtp

getservbyport()
#include <netdb.h>
struct servent *getservbyport(int port, const char *proto);


   21      portnum = atoi(argv[1]);
   22      if ( ( serv = getservbyport(htons(portnum),"tcp")) == NULL ){
   23          perror("getservbyport:");
   24          exit(1);
   25      }


struct servent
{




};
   26      printf( "port %d is %s\n", portnum , serv->s_name );
   27      exit(0);

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS