int main(void) { int i; vector v; vector_init(&v); // char temp1[20] vector_add(&v, 1); vector_add(&v, 2); vector_add(&v, 3); vector_add(&v, 4); for (i = 0; i < vector_total(&v); i++) printf("%s ", (char *) vector_get(&v, i)); printf("\n"); vector_delete(&v, 3); vector_delete(&v, 2); vector_delete(&v, 1); vector_set(&v, 0, "Hello"); vector_add(&v, "World"); for (i = 0; i < vector_total(&v); i++) printf("%s ", (char *) vector_get(&v, i)); printf("\n"); vector_free(&v); }
int main(void) { int i; vector v; vector_init(&v); vector_add(&v, "Bonjour"); vector_add(&v, "tout"); vector_add(&v, "le"); vector_add(&v, "monde"); for (i = 0; i < vector_total(&v); i++) printf("%s ", (char *) vector_get(&v, i)); printf("\n"); vector_delete(&v, 3); vector_delete(&v, 2); vector_delete(&v, 1); vector_set(&v, 0, "Hello"); vector_add(&v, "World"); for (i = 0; i < vector_total(&v); i++) printf("%s ", (char *) vector_get(&v, i)); printf("\n"); vector_free(&v); }
void receive_user_data(void) { char buf[BUF_SIZE] = {0}; char message[BUF_SIZE] = {0}; char *name; Client *client; int read_bytes = read(0, buf, BUF_SIZE); assert(read_bytes < BUF_SIZE); if (read_bytes == -1) { warn("Could not read from STDIN data."); return; } if (read_bytes == 1) { /* read only LF symbol */ info("Could not send empty message."); return; } /* remove last LF symbol readed from stdin */ buf[read_bytes - 1] = '\0'; switch(parse_user_command(buf)) { case KILL: name = get_command_argument(buf); if (!name) { warn("Name of client should be provided."); break; } client = vector_delete_first_equal(_s_clients, name, cmp_clients_by_name); info("Server has '%d' clients.", vector_total(_s_clients)); if (client) { sprintf(message, "The client '%s' left the chat.", client->name); send_broadcast_msg(message); info("%s", message); /* send info data to client */ sprintf(message, "server: You was killed."); send_msg(client, message); client_destroy(client); } else { info("Client '%s' was not found.", name); } break; case SHUTDOWN: sprintf(message, "The server is going shutdown. Bye."); send_broadcast_msg(message); _s_runing = 0; info("%s", message); break; case WHO: vector_foreach(_s_clients, message, collect_all_names); info("All clients: '%s'", message); break; default: warn("Unknown command. Allowed commands: _kill <name>, _shutdown, _who"); break; } }
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."); }