/********************************************************************************* * Copyright: (C) 2012 Guo Wenxue * All rights reserved. * * Filename: date_time_server.c * Description: This is a sample socket server demo program. * * Version: 1.0.0(06/08/2012~) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "06/08/2012 02:50:51 PM" * ********************************************************************************/ /* Some Unix Program Standard head file */ #include #include #include #include #include #include /* getopt_long */ #include /* getopt_long */ /* Socket Program head file */ #include #include #include /* sockaddr_in{} and other Internet define */ /* strerror(), perror(), errno head file*/ #include #include #define LISTENQ 1024 /* 2nd argument to listen () */ #define DEF_USER "lingyun" #define DEF_PASSWD "www.iot-yun.com" void *socket_client_worker(void *arg); void print_usage(char *progname) { printf("Usage: %s [OPTION]...\n", progname); printf(" %s is a socket server program, which used to verify client and echo back string from it\n", progname); printf("\nMandatory arguments to long options are mandatory for short options too:\n"); printf(" -b[daemon ] set program running on background\n"); printf(" -p[port ] Socket server port address\n"); printf(" -h[help ] Display this help information\n"); printf("\nExample: %s -b -p 8900\n", progname); return ; } int main(int argc, char **argv) { int listenfd, connfd; struct sockaddr_in servaddr; int serv_port = 0; int daemon_run = 0; char *progname = NULL; pthread_t tid; int opt; int on=1; struct option long_options[] = { {"daemon", no_argument, NULL, 'b'}, {"port", required_argument, NULL, 'p'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; progname = basename(argv[0]); /* Parser the command line parameters */ while ((opt = getopt_long(argc, argv, "bp:h", long_options, NULL)) != -1) { switch (opt) { case 'b': daemon_run=1; break; case 'p': serv_port = atoi(optarg); break; case 'h': /* Get help information */ print_usage(progname); return EXIT_SUCCESS; default: break; } } if(0==serv_port) { print_usage(progname); return -1; } /* set program running on background */ if( daemon_run ) { daemon(0, 0); } /* * Open an IPV4(AF_INET) TCP(SOCK_STREAM) Socket File Description(listenfd), UDP socket * should use SOCK_DGRAM,We can use linux command "man socket" to see this function manual */ if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { /* strerror() is the 1st way to display the failure reason, argument * errno is a globle variable defined in , we can use linux command * "man strerror" to see this function manual*/ printf("Use socket() to create a TCP socket failure: %s\n", strerror(errno)); return -1; } /* Set socket port reuseable */ setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); /* Now we set the Server Information, include IPV4 or IPV6, Listen IP address and Port */ memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; /* Set it as IPV4 protocal */ servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Listen all the local IP address */ servaddr.sin_port = htons(serv_port); /* daytime server port */ /* * When a socket is created with socket(2), it exists in a name space (address family) but * has no address assigned to it. bind() assigns the address specified to by addr to the * socket referred to by the file descriptor listenfd. We can use Linux command "man 2 bind" * to see this function manual. */ if(bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { printf("Use bind() to bind the TCP socket failure: %s\n", strerror(errno)); goto CleanUp; } /* * listen() marks the socket referred to by listenfd as a passive socket, that is, as a socket * that will be used to accept incoming connection requests using accept(2). We can use Linux * command "man listen" to see this function manual. */ if(listen(listenfd, LISTENQ) < 0) { printf("Use bind() to bind the TCP socket failure: %s\n", strerror(errno)); goto CleanUp; } printf("%s server start to listen port %d\n", argv[0],serv_port); for ( ; ; ) { /* * The accept() system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET). * It extracts the first connection request on the queue of pending connections for the listening * socket linstenfd, creates a new connected socket(connfd), and returns a new file descriptor referring * to that socket. The newly created socket is not in the listening state. The original socket * listenfd is unaffected by this call. */ if( (connfd=accept(listenfd, (struct sockaddr *)NULL, NULL)) > 0) { pthread_create(&tid, NULL, socket_client_worker, (void *)connfd); } } CleanUp: close(listenfd); /* We must close socket File Description when program exit*/ return 0; } static inline msleep(unsigned long ms) { struct timeval tv; tv.tv_sec = ms/1000; tv.tv_usec = (ms%1000)*1000; select(0, NULL, NULL, NULL, &tv); } char *fail_str[3]={"Verify user account get wrong format", "Verify user account get wrong username", "Verify user account get wrong password"}; int verify_user_logon(char *buf) { char *ptr = NULL; if( !(ptr=strchr(buf, ':')) ) { return 1; } if( strncmp(buf, DEF_USER, strlen(DEF_USER)) ) { return 2; } if( strncmp(ptr+1, DEF_PASSWD, strlen(DEF_PASSWD)) ) { return 3; } return 0; } void *socket_client_worker(void *arg) { int client_fd; char buf[512]; int rv; if( !arg ) { printf("Invalid arg\n"); return NULL; } client_fd = (int) arg; #ifdef CONFIG_VERIFY_USER memset(buf, 0, sizeof(buf)); if( (rv=read(client_fd, buf, sizeof(buf))) <= 0) { printf("socket disconncet or read get error\n"); goto CleanUp; } if( 0 != (rv=verify_user_logon(buf)) ) { printf("client logon account [%s] invalid, disconncet it now\n", buf); write(client_fd, fail_str[rv-1], strlen(fail_str[rv-1])); goto CleanUp; } write(client_fd, "passed", 6); #endif while(1) { memset(buf, 0, sizeof(buf)); if( (rv=read(client_fd, buf, sizeof(buf))) <= 0) { printf("socket disconncet or read get error\n"); goto CleanUp; } /* echo the data from client back */ printf("socket[%d] read get %d bytes data and will echo it back\n", client_fd, rv); write(client_fd, buf, rv); } CleanUp: printf("Thread worker for socket[%d] exit.\n", client_fd); msleep(500); close(client_fd); }