int main(int argc, char** argv) { Connection *server = NULL; Client *client = NULL; fd_set readf; int maxfd; int server_socket; int client_fd; int i; int ready; int amount; args_parse(argc, argv); if (_s_server_port < 0 || _s_server_port > 65535) { _s_server_port = SERVER_PORT; } if (_s_con_type == TCP) { info("Server will use TCP connection."); } else if (_s_con_type == UDP) { info("Server will use UDP connection."); } else { fatal("You should provide connection type."); } server = connection_server_create(_s_con_type, "0.0.0.0", _s_server_port, MAX_AMOUNT_CLIENTS); server_socket = connection_get_fd(server); _s_clients = vector_create(); info("Server has started on port '%d'.", _s_server_port); info("Allowed commands:" "\n\t1) _who" "\n\t2) _kill <name>" "\n\t3) _shutdown"); _s_runing = 1; while(_s_runing) { FD_ZERO(&readf); maxfd = server_socket; if (connection_get_type(server) == TCP) { for (i = 0; i < vector_total(_s_clients); ++i) { client = vector_get(_s_clients, i); client_fd = connection_get_fd(client->conn); FD_SET(client_fd, &readf); if (client_fd > maxfd) { maxfd = client_fd; } } } FD_SET(server_socket, &readf); FD_SET(0, &readf); ready = select(maxfd + 1, &readf, 0, 0, 0); /* read data from user terminal */ if (FD_ISSET(0, &readf)) { receive_user_data(); if (--ready <= 0) { /* no more descriptors for handling */ continue; } } /* got something on server connection */ if (FD_ISSET(server_socket, &readf)) { /* new client connection */ handle_server_event(server); if (--ready <= 0) { /* no more descriptors for handling */ continue; } } if (connection_get_type(server) == TCP) { /* read data from clients */ amount = vector_total(_s_clients); for (i = 0; i <= amount; ++i) { /* check all clients for data */ client = vector_get(_s_clients, i); if (client == NULL) { assert("Client is NULL" == NULL); continue; } if (FD_ISSET(connection_get_fd(client->conn), &readf)) { receive_tcp_client_data(client); if (--ready <= 0) { /* no more descriptors for handling */ break; } } } } } info("Close all sockets and removing all clients..."); close(server_socket); amount = vector_total(_s_clients); for (i = 0; i <= amount; ++i) { if ((client = vector_get(_s_clients, i)) != NULL) { close(connection_get_fd(client->conn)); client_destroy(client); }; } vector_free(_s_clients); info("Done."); }
int main(int argc, char *argv[]) { int sfd, n; struct sockaddr_in servaddr; sfd = socket(AF_INET, SOCK_STREAM, 0); if (sfd < 0) { printf("Socket could not be created!\n"); exit(1); } fd_handlers_add(sfd); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(5000); n = bind(sfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); if (n != 0) { perror("bind"); exit(1); } if(listen(sfd, 10) != 0) { perror("listen"); } printf("Start listening...\n"); for (;;) { int rv; fd_set rfds; /* build fd set */ fd_handlers_build_fd_set(&rfds); /* get max fd */ int max_fd = fd_handlers_get_max_fd(&rfds); /* with select we look at all the read file descriptors */ rv = select(max_fd + 1, &rfds, NULL, NULL, NULL); if (rv < 0) { /* select error! */ perror("select"); } else if (rv > 0) { /* something happened! */ int i; printf("Data is available now.\n"); for(i = 0; i < MAX_FD_HANDLERS; i++) { if(fd_handlers[i] == 0) continue; /* FD_ISSET returns true if something happened on the file descriptor which *the select function noticed, false otherwise */ if(FD_ISSET(fd_handlers[i], &rfds)) { if (fd_handlers[i] == sfd) { /* handle new incoming connection with accept*/ handle_server_event(sfd); } else { /* handle client request */ handle_client_event(fd_handlers[i]); } } } } } close(sfd); return 0; }