Esempio n. 1
0
void processMsg(int sockfd, struct sockaddr_in addr, struct chatmsg msg) {
    if (msg.msgtype == CHATMSG_JOIN) {
        adduser(addr, msg.userid);
    } else if (msg.msgtype == CHATMSG_CHAT) {
        sendtouser(sockfd, msg);
    } else if (msg.msgtype == CHATMSG_HEART) {
        islive[msg.userid] = time(NULL);
    }
}
Esempio n. 2
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;
}