//------------------------- verica informacoes de login ----------------------- void login(int clientsocket) { msg_t *msg; msg = (msg_t *) malloc (sizeof (msg_t)); recv(clientsocket, msg, sizeof(msg_t), 0); int count; // compara informacoes de login com a lista de usuarios (users) for (count = 0; count < 3; count++) { if (!strcmp(msg->user, users[count][0]) && !strcmp(msg->msgbuff, users[count][1])) { // login aceito, informa o cliente printf("Usuario '%s' conectou-se.\n", msg->user); strcpy(conusers[conclients], msg->user); strcpy(msg->user, "SERVER"); strcpy(msg->msgbuff, "Login aceito, conexao estabelecida."); msg->msgtype = Login; send(clientsocket, msg, sizeof(msg_t), 0); //informa os demais clientes conectados sobre o novo usuario msg->msgtype = Message; strcpy(msg->msgbuff, "Usuario '"); strcat(msg->msgbuff, conusers[conclients]); strcat(msg->msgbuff, "' conectou-se."); sendtoall(msg); // adiciona o o novo cliente a lista de sockets sockets[conclients].fd = clientsocket; conclients++; return; } } // login recusado, informa o cliente strcpy(msg->user, "SERVER"); strcpy(msg->msgbuff, "Nome de usuario ou senha invalido, conexao rejeitada."); msg->msgtype = Logout; send(clientsocket, msg, sizeof(msg_t), 0); return; }
//----------------------- funcao principal do programa ------------------------ int main(int argc, char *argv[]) { // usa porta predefinida port = SERVERPORT; // configura socket do servidor setup_server(); // inicializa estrutura para verificacao de eventos de leitura no socket int pollret; for (pollret = 0; pollret < MAXCONCLIENTS; pollret++) { sockets[pollret].fd = -1; sockets[pollret].events = POLLIN | POLLPRI; } conclients = 0; // thread p aguardar e estabelecer novas conexoes... if (pthread_create(&thread, NULL, acceptconnections, 0)) fatal("thread_creation: %s.", strerror(errno)); // recebe mensagens dos clientes e as reenvia para todos os clientes // conectados, enquanto houver clientes conectados msg_t *msg; msg = (msg_t *) malloc (sizeof (msg_t)); int count; do { // aguarda por evento nos sockets while (!conclients || !(pollret = poll(sockets, conclients, 1000))); // verifica os eventos ocorridos nos sockets dos clientes for (count = 0; count < conclients; count++) { msg->msgtype = Message; // se ha conteudo a ser lido no socket if ( (sockets[count].revents & POLLIN) || (sockets[count].revents & POLLPRI)) { // recebe msg do cliente e reenvia a todos recv(sockets[count].fd, msg, sizeof(msg_t), 0); if (msg->msgtype != Logout) { printf("%s envia: %s\n", msg->user, msg->msgbuff); sendtoall(msg); } } // se cliente solicitou logout ou ocorreu erro com seu socket if ( (sockets[count].revents & POLLHUP) || (sockets[count].revents & POLLNVAL) || msg->msgtype == Logout ) { // gera msg para avisar sobre a saida do usuario printf("Usuario '%s' desconectou-se.\n", conusers[count]); strcpy(msg->user, "SERVER"); strcpy(msg->msgbuff, "Usuario '"); strcat(msg->msgbuff, conusers[count]); strcat(msg->msgbuff, "' desconectou-se."); msg->msgtype = Message; // remove usuario removeclientat(count); // informa os demais da saida do usuario sendtoall(msg); // cliente desconectado, retorna o contador count--; // se nao ha clientes conectados, finaliza o servidor if (!conclients) conclients = -1; } } } while (conclients != -1); // encerra o socket do servidor close(serversock); return 0; }
int main(int argc, char **argv) { int sockfd, userlen; printf ("********* Welcome to mini chat cote client *********\n"); printf( " Les commande sont : \n" " login puis nom utilisateur pour connecter au serveur \n" " pour envoyer un message a un client taper : send utilisateur puis message \n" " quiter \n\n\n"); printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*Client*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n"); memset(&me, 0, sizeof (struct USER)); while (gets(comande)) { if (!strncmp(comande, "quiter", 4)) { logout(&me); break; } /* if (!strncmp(comande, "help", 4)) { FILE *fin = fopen("help.txt", "r"); if (fin != NULL) { while (fgets(comande, LINEBUFF - 1, fin)) puts(comande); fclose(fin); } else { fprintf(stderr, "Help file not found...\n"); } }*/ else if (!strncmp(comande, "login", 5)) { char *ptr = strtok(comande, " "); ptr = strtok(0, " "); memset(me.user, 0, sizeof (char) * USERLEN); if (ptr != NULL) { userlen = strlen(ptr); if (userlen > USERLEN) ptr[USERLEN] = 0; strcpy(me.user, ptr); } else { strcpy(me.user, "Anonymous"); } login(&me); } else if (!strncmp(comande, "user", 5)) { char *ptr = strtok(comande, " "); ptr = strtok(0, " "); memset(me.user, 0, sizeof (char) * USERLEN); if (ptr != NULL) { userlen = strlen(ptr); if (userlen > USERLEN) ptr[USERLEN] = 0; strcpy(me.user, ptr); setuser(&me); } } else if (!strncmp(comande, "send", 5)) { char *ptr = strtok(comande, " "); char temp[USERLEN]; ptr = strtok(0, " "); memset(temp, 0, sizeof (char) * USERLEN); if (ptr != NULL) { userlen = strlen(ptr); if (userlen > USERLEN) ptr[USERLEN] = 0; strcpy(temp, ptr); while (*ptr) ptr++; ptr++; while (*ptr <= ' ') ptr++; sendtouser(&me, temp, ptr); } } else if (!strncmp(comande, "send", 4)) { sendtoall(&me, &comande[5]); } else if (!strncmp(comande, "logout", 6)) { logout(&me); } else fprintf(stderr, "Unknown comande...\n"); } return 0; }
int client(int argc, char *argv[]) { int sockfd, aliaslen; int portno = atoi(argv[2]); portnom=portno; ipcon = gethostbyname(argv[1]); memset(&me, 0, sizeof(struct USER)); //Komendy while(gets(option)) { if(!strncmp(option, "exit", 4)) { logout(&me); break; } if(!strncmp(option, "help", 4)) { FILE *fin = fopen("help.txt", "r"); if(fin != NULL) { while(fgets(option, LINEBUFF-1, fin)) puts(option); fclose(fin); } else { fprintf(stderr, "Plik pomocy nie znaleziony...\n"); } } else if(!strncmp(option, "login", 5)) { char *ptr = strtok(option, " "); ptr = strtok(0, " "); memset(me.alias, 0, sizeof(char) * ALIASLEN); if(ptr != NULL) { aliaslen = strlen(ptr); if(aliaslen > ALIASLEN) ptr[ALIASLEN] = 0; strcpy(me.alias, ptr); } else { strcpy(me.alias, "Anonymous"); } login(&me); } else if(!strncmp(option, "alias", 5)) { char *ptr = strtok(option, " "); ptr = strtok(0, " "); memset(me.alias, 0, sizeof(char) * ALIASLEN); if(ptr != NULL) { aliaslen = strlen(ptr); if(aliaslen > ALIASLEN) ptr[ALIASLEN] = 0; strcpy(me.alias, ptr); setalias(&me); } } else if(!strncmp(option, "whisp", 5)) { char *ptr = strtok(option, " "); char temp[ALIASLEN]; ptr = strtok(0, " "); memset(temp, 0, sizeof(char) * ALIASLEN); if(ptr != NULL) { aliaslen = strlen(ptr); if(aliaslen > ALIASLEN) ptr[ALIASLEN] = 0; strcpy(temp, ptr); while(*ptr) ptr++; ptr++; while(*ptr <= ' ') ptr++; sendtoalias(&me, temp, ptr); } } else if(!strncmp(option, "send", 4)) { sendtoall(&me, &option[5]); } else if(!strncmp(option, "logout", 6)) { logout(&me); } else fprintf(stderr, "Zla komenda...\n"); } return 0; }
int main(int argc, char **argv) { int sockfd, userlen; memset(&me, 0, sizeof(struct USER)); while(gets(option)) { if(!strncmp(option, "exit", 4)) { logout(&me); break; } else if(!strncmp(option, "login", 5)) { char *ptr = strtok(option, " "); ptr = strtok(0, " ");//set ptr as username memset(me.user, 0, sizeof(char) * userLEN); if(ptr != NULL) { userlen = strlen(ptr);//strlen:calcule la longeur du chaine de caractere if(userlen > userLEN) ptr[userLEN] = 0; strcpy(me.user, ptr);//copy ptr in me.user } else { strcpy(me.user, "default");//copy default to me.user } login(&me); } else if(!strncmp(option, "change", 6)) { char *ptr = strtok(option, " "); ptr = strtok(0, " "); memset(me.user, 0, sizeof(char) * userLEN); if(ptr != NULL) { userlen = strlen(ptr); if(userlen > userLEN) ptr[userLEN] = 0; strcpy(me.user, ptr); setuser(&me); } } else if(!strncmp(option, "specf", 5)) { char *ptr = strtok(option, " "); char temp[userLEN]; ptr = strtok(0, " "); memset(temp, 0, sizeof(char) * userLEN); if(ptr != NULL) { userlen = strlen(ptr); if(userlen > userLEN) ptr[userLEN] = 0; strcpy(temp, ptr); while(*ptr) ptr++; ptr++; while(*ptr <= ' ') ptr++; sendtoclient(&me, temp, ptr); } } else if(!strncmp(option, "sfile", 5)) { char *ptr = strtok(option, " "); char temp[userLEN]; char *buff; ptr = strtok(0, " "); memset(temp, 0, sizeof(char) * userLEN); if(ptr != NULL) { userlen = strlen(ptr); if(userlen > userLEN) ptr[userLEN] = 0; strcpy(temp, ptr); while(*ptr) ptr++; ptr++; while(*ptr <= ' ') ptr++; sendfile(&me, temp, ptr,buff); } } else if(!strncmp(option, "encrypt", 7)) { char *ptr = strtok(option, " "); char temp[userLEN]; ptr = strtok(0, " "); memset(temp, 0, sizeof(char) * userLEN); if(ptr != NULL) { userlen = strlen(ptr); if(userlen > userLEN) ptr[userLEN] = 0; strcpy(temp, ptr); while(*ptr) ptr++; ptr++; while(*ptr <= ' ') ptr++; sendencrypt(&me, temp, ptr); } } else if(!strncmp(option, "broadcast", 9)) { sendtoall(&me, &option[9]); } else if(!strncmp(option, "list", 4)) { } else if(!strncmp(option, "logout", 6)) { logout(&me); } else fprintf(stderr, "Unknown option...\n"); } return 0; }