getport

programming/C_C++ 2011. 9. 1. 15:05
반응형
#include <stdio.h>
#include <netdb.h>

int getport(char *port)
{
  struct servent *sp;
  sp = getservbyname(port, "tcp");
  if (sp == NULL) {
    return (atoi(port));
  }
  else {
    return (ntohs(sp->s_port));
  }
}


int main(int argc, char *argv[])
{
  if( argc != 2) {
    printf("Invalid argument \n");
    exit(0);
  }
  printf("%d\n", getport(argv[1]) );
}


 실행결과 

# getport 21 
21

# getport ftp
21


# getport 23
23


# getport telnet
23
 
반응형
Posted by 공간사랑
,