add option base

This commit is contained in:
ubuntu201711081 2020-12-04 09:04:46 +00:00
parent 9ff0082313
commit 61830ba554
2 changed files with 81 additions and 4 deletions

View File

@ -173,6 +173,39 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
#endif #endif
return 0; 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; static int sock;
void safe_exit(){ void safe_exit(){
close(sock); close(sock);
@ -185,6 +218,11 @@ int main(int argc, const char *argv[]){
int csock; int csock;
int bufsize; int bufsize;
int i; 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); sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit); atexit(safe_exit);
if(sock < 0){ 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_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
addr.sin_family = AF_INET; 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){ if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
perror("bind failed"); perror("bind failed");
@ -215,7 +253,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN]; char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf)); const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
assert(msg != NULL); 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){ if(listen(sock,MAX_LISTEN_SOCKET) < 0){
perror("listen failed"); perror("listen failed");

View File

@ -171,6 +171,39 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
#endif #endif
return 0; 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; static int sock;
void safe_exit(){ void safe_exit(){
@ -183,6 +216,12 @@ int main(int argc, const char *argv[]){
socklen_t client_addr_len = sizeof(client_addr); socklen_t client_addr_len = sizeof(client_addr);
int csock; int csock;
int bufsize; 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); sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit); atexit(safe_exit);
if(sock < 0){ 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_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
addr.sin_family = AF_INET; 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){ if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
perror("bind failed"); perror("bind failed");
@ -213,7 +252,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN]; char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf)); const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
assert(msg != NULL); 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){ if(listen(sock,1) < 0){