int main(int argc, char *argv[]) { int i; init(); /* Init some variables (like malloc timestamp string, encrypt text string, etc.) */ check_par(argc, argv); /* Check command arguments number */ open_config(argv); /* Open config file and check if it failed */ open_log(argv); /* Open log file and check if it failed */ get_ipaddr(); /* Get server IP address */ create_socket(); /* Create a socket */ bind_socket(); /* Bind the socket */ listen_socket(); /* Listen at the socket */ print_server_info(); /* Print server information */ while (TRUE) { /* Read until the end of file */ if (read_flag) { if (fscanf(fcfg, "%s", enc_txt) == EOF) { finish_flag = 1; break; } else { fscanf(fcfg, "%s", dec_txt); } } read_flag = 0; init_select(); /* Select function */ if (select_func() == -1) break; for (i = 0; i < max_fds + 1; i++) { if (FD_ISSET(i, &rfds)) { if (i == sockfd) { /* If have a new client connect */ if (accept_new_cli() == -1) break; /* Try to accept new client */ if (check_connect() == -1) break; /* Check connect message from client */ if (print_client_info() == -1) break; /* Print the information of client side */ store_client_ip(); /* Store the client ip address */ break; } else { /* If have new message from client side */ client_ip = get_host_by_sockfd(i); /* Get the client ip address by socket */ recv_socket_msg(i, recv_mark); /* Get the message from socket */ handle_client_msg(i); /* Handle client message (SUCCESS_MSG, FAILURE_MSG, DISPATCH_MSG, etc.) */ break; } } if (main_flag == EXIT_FAILURE) break; } if (main_flag == EXIT_FAILURE) break; } remained_cli = ask_clients_quit(); /* Ask clients quit and count the remain clients number */ wait_clients_quit(); /* Wait for all clients quit */ quit_server(); /* Clean up and quit server, also print the message to log */ return main_flag; }
void server() { std::cout << "Server" << std::endl; int server_fd = sock_listen("12345", 10, NULL); if ( server_fd == -1 ) { std::cout << "sock_listen error " << errno << " " << strerror( errno ) << std::endl; exit(1); } threadpool tp(10); while (true) { sockaddr_storage client_addr; socklen_t len = sizeof(client_addr); int client_fd = accept(server_fd, (sockaddr*)&client_addr, &len); tp.add_task( [client_fd] () { print_client_info("New client - ", client_addr); std::cout<<std::endl; char buf[BUF_SIZE]; memset(buf, 0, sizeof(buf) ); ssize_t num_recv = recv(client_fd, buf, BUF_SIZE, 0); std::cout << "num_recv " << num_recv << " " << buf << std::endl; sleep(2); strncpy(buf, "abcdef", BUF_SIZE); ssize_t num_sent = send(client_fd, buf, strlen(buf)+1, 0); std::cout << "num_sent " << num_sent << std::endl; close( client_fd ); }); } }
void *parse_line () { int caret=0; char line[MAX_LINE], parameter[MAX_LINE]; /* if parameter is set as smaller than MAX_LINE, it is necessary to implement buffer overrun protection !!! */ char *remainingString; while( fgets(line,MAX_LINE,stdin) != NULL ) { remainingString = line; while( sscanf(remainingString,"%s%n",parameter,&caret) > 0 ) { remainingString += caret; /* moves along the line */ if( !strcmp(parameter,"M") ) { print_client_info(); } else if( !strcmp(parameter,"HELP") ) { FILE *helpFile; char file_line[MAX_LINE]; helpFile = fopen("./doc/ServerHelpFile.txt","r"); if( helpFile == NULL ) { fprintf(stdout,":: Error!\n:: Can't open help file.\n:: Check your working directory.\n"); } else { while( fgets(file_line,MAX_LINE,helpFile) != NULL ) { fprintf(stdout,"%s",file_line); } fclose(helpFile); } } else if( !strcmp(parameter,"V") ) { VRB++; /* test is done with bitwise AND; true for DBG odd, false for even */ if( VRB & 1 ) { fprintf(stdout,":: Verbose mode ON\n"); } else { fprintf(stdout,":: Verbose mode OFF\n"); } } else if( !strcmp(parameter,"F") ) { master_switch(); } else { fprintf(stdout,":: Parsing error!\n:: Unknown command \"%s\" ignored.\n:: Try HELP for assistance.\n",parameter); } } } pthread_exit(NULL); }
int main() { struct sockaddr_in serv_addr; bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERVER_PORT); serv_addr.sin_addr.s_addr = htonl(SERVER_IP); int listen_sock = socket(AF_INET, SOCK_STREAM, 0); bind(listen_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); listen(listen_sock, 10); client_fd[0].fd = listen_sock; client_fd[0].events = POLLRDNORM; int i; for (i = 1; i < OPEN_MAX; i++) { client_fd[i].fd = -1; req[i].n = -1; } maxi = 0; int nready; struct sockaddr_in client_addr; int len, n, ncmd; int conn; char buf[1024], err[64]; int sock; FILE* fp; for ( ; ; ) { nready = poll(client_fd, maxi + 1, -1); // check listening socket if (client_fd[0].revents & POLLRDNORM) { len = sizeof(client_addr); conn = accept(listen_sock, (struct sockaddr*)&client_addr, &len); for (i = 1; i < OPEN_MAX; i++) if (client_fd[i].fd < 0) { client_fd[i].fd = conn; break; } if (i == OPEN_MAX) { perror("OPEN_MAX reached"); exit(0); } client_fd[i].events = POLLRDNORM; if (i > maxi) maxi = i; print_client_info(&client_addr); if (--nready <= 0) continue; } // check connected socket for (i = 1; i <= maxi; i++) { if ((sock = client_fd[i].fd) < 0) continue; if (client_fd[i].revents & (POLLRDNORM | POLLERR)) { n = read(sock, &req[i].n, 4); if (n == 0) { printf("client closed\n"); close(client_fd[i].fd); client_fd[i].fd = -1; req[i].n = -1; } else if (n < 0) { perror("read error"); close(client_fd[i].fd); client_fd[i].fd = -1; req[i].n = -1; } else if (n < 4) { // deal with partial read sprintf(err, "read count error: read %d, return %d", 4, n); perror(err); exit(0); } else { n = read(sock, req[i].cmd, req[i].n); if (n != req[i].n) { sprintf(err, "read cmd error: read %d, return %d", req[i].n, n); perror(err); exit(0); } req[i].cmd[req[i].n] = '\0'; printf("receive command: %s\n", req[i].cmd); fp = popen(req[i].cmd, "r"); if (!fp) { perror("popen error"); exit(0); } while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { write(client_fd[i].fd, buf, n); } pclose(fp); } if (--nready <= 0) break; } } } return 0; }