From 61830ba554c3d598f36a1ebecd7281638c293095 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Fri, 4 Dec 2020 09:04:46 +0000 Subject: [PATCH] add option base --- p-server.c | 42 ++++++++++++++++++++++++++++++++++++++++-- server.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/p-server.c b/p-server.c index 3f1731a..1d5f791 100644 --- a/p-server.c +++ b/p-server.c @@ -173,6 +173,39 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ #endif return 0; } +const char * help(const char * n){ + const char * msg = "USASE : %s [Option] ...\n" + "Options and arguments: \n" + "-p port\t:set to port binding. couldn't set to 0\n" + "-h\t:print help message.\n"; + printf(msg,n); + return msg; +} +/** return 0 ok. otherwise invalid format*/ +int parse_args(int argc,const char * argv[] , in_port_t * port){ + int pos = 1; + const char * opt; + while (pos < argc) + { + opt = argv[pos++]; + if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0) + { + help(argv[0]); + return 2; + } + else if(strcmp(opt,"-p") == 0 || strcmp(opt,"--port") == 0){ + if (pos < argc){ + const char * value = argv[pos++]; + *port = atoi(value); + if (port == 0){ // either not number or zero + return 2; + } + } + else return 2; //failed to find argument. + } + } + return 0; +} static int sock; void safe_exit(){ close(sock); @@ -185,6 +218,11 @@ int main(int argc, const char *argv[]){ int csock; int bufsize; int i; + in_port_t binding_port_number = SERVER_PORT; + if (argc > 1){ + int d = parse_args(argc,argv,&binding_port_number); + if(d != 0 ) return d; + } sock = socket(AF_INET,SOCK_STREAM,0); atexit(safe_exit); if(sock < 0){ @@ -206,7 +244,7 @@ int main(int argc, const char *argv[]){ addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/ addr.sin_family = AF_INET; - addr.sin_port = htons(SERVER_PORT); + addr.sin_port = htons(binding_port_number); if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){ perror("bind failed"); @@ -215,7 +253,7 @@ int main(int argc, const char *argv[]){ char ip_buf[INET_ADDRSTRLEN]; const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf)); assert(msg != NULL); - fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT); + fprintf(stderr,"server bind on %s:%d\n",msg ,binding_port_number); } if(listen(sock,MAX_LISTEN_SOCKET) < 0){ perror("listen failed"); diff --git a/server.c b/server.c index c0c6e0a..f606627 100644 --- a/server.c +++ b/server.c @@ -171,6 +171,39 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ #endif return 0; } +const char * help(const char * n){ + const char * msg = "USASE : %s [Option] ...\n" + "Options and arguments: \n" + "-p port\t:set to port binding. couldn't set to 0\n" + "-h\t:print help message.\n"; + printf(msg,n); + return msg; +} +/** return 0 ok. otherwise invalid format*/ +int parse_args(int argc,const char * argv[] , in_port_t * port){ + int pos = 1; + const char * opt; + while (pos < argc) + { + opt = argv[pos++]; + if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0) + { + help(argv[0]); + return 2; + } + else if(strcmp(opt,"-p") == 0 || strcmp(opt,"--port") == 0){ + if (pos < argc){ + const char * value = argv[pos++]; + *port = atoi(value); + if (port == 0){ // either not number or zero + return 2; + } + } + else return 2; //failed to find argument. + } + } + return 0; +} static int sock; void safe_exit(){ @@ -183,6 +216,12 @@ int main(int argc, const char *argv[]){ socklen_t client_addr_len = sizeof(client_addr); int csock; int bufsize; + in_port_t binding_port_number = SERVER_PORT; + if (argc > 1){ + int d = parse_args(argc,argv,&binding_port_number); + if(d != 0 ) return d; + } + sock = socket(AF_INET,SOCK_STREAM,0); atexit(safe_exit); if(sock < 0){ @@ -204,7 +243,7 @@ int main(int argc, const char *argv[]){ addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/ addr.sin_family = AF_INET; - addr.sin_port = htons(SERVER_PORT); + addr.sin_port = htons(binding_port_number); if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){ perror("bind failed"); @@ -213,7 +252,7 @@ int main(int argc, const char *argv[]){ char ip_buf[INET_ADDRSTRLEN]; const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf)); assert(msg != NULL); - fprintf(stderr,"server bind on %s:%d\n",msg ,SERVER_PORT); + fprintf(stderr,"server bind on %s:%d\n",msg ,binding_port_number); } if(listen(sock,1) < 0){