Skip to content

Commit

Permalink
add usage infomation, add deamonize mode, add port in use checking
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhuai committed Jun 13, 2017
1 parent fcff96c commit df6f682
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions example_echosvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,24 @@ static int CreateTcpSocket(const unsigned short shPort /* = 0 */,const char *psz

int main(int argc,char *argv[])
{
if(argc<5){
printf("Usage:\n"
"example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT]\n"
"example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT] -d # daemonize mode\n");
return -1;
}
const char *ip = argv[1];
int port = atoi( argv[2] );
int cnt = atoi( argv[3] );
int proccnt = atoi( argv[4] );
bool deamonize = argc >= 6 && strcmp(argv[5], "-d") == 0;

g_listen_fd = CreateTcpSocket( port,ip,true );
listen( g_listen_fd,1024 );
if(g_listen_fd==-1){
printf("Port %d is in use\n", port);
return -1;
}
printf("listen %d %s:%d\n",g_listen_fd,ip,port);

SetNonBlock( g_listen_fd );
Expand Down Expand Up @@ -233,6 +244,7 @@ int main(int argc,char *argv[])

exit(0);
}
if(!deamonize) wait(NULL);
return 0;
}

0 comments on commit df6f682

Please sign in to comment.