Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    [ C ] Utenti Connessi in C

    ragazzi ho creato questo programma in c e ho un piccolo problema di logica, potete controllare quale sia:

    codice:
    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    #include <time.h>
    #define NOME_FILE "uc.bin"
    #define ERRORE_USCITA "Utenti connessi: 1"
    
    struct strDati {
     char ipUtente[20];
     int oraAccesso;
    };
    
    typedef struct strContDati {
      struct strDati impoUtente;
      struct strContDati* pStesso;
    } impoUtenti;
    
    void liberaMemoria(impoUtenti*);
    
    FILE* fp=NULL;
    int contaUtenti=0;
    int verificaUtente=0;
    
    int main(int argc, char *argv[]) {
    
      // dati necessari dell'utente
      char* ipUtenteTmp="139.0.0.1"; //getenv("REMOTE_ADDR");
      int oraAccessoTmp=time('\0');
    
      impoUtenti* Lista=(impoUtenti*)malloc(sizeof(impoUtenti));
      impoUtenti* cLista=Lista;
    
      printf("content-type:text/html\n\n");
      // esci se non ci sono i dati necessari
      if(!ipUtenteTmp) {
       printf("%s", ERRORE_USCITA);
       free(Lista);
       exit(1);
      }
    
      if((fp=fopen(NOME_FILE, "rb"))==NULL) {
       printf("%s", ERRORE_USCITA);
       free(Lista);
       exit(1);
      }
    
      if(!feof(fp)) {
    
       while(!feof(fp)) {
        fread(Lista, sizeof(impoUtenti), 1, fp);
        if(!strcmp(Lista->impoUtente.ipUtente, ipUtenteTmp)) {
         Lista->impoUtente.oraAccesso=oraAccessoTmp;
         verificaUtente=1;
        }
    
        if(!feof(fp)) {
         Lista->pStesso=(impoUtenti*)malloc(sizeof(impoUtenti));
         Lista=Lista->pStesso;
        } else {
         Lista->pStesso=NULL;
        }
    
       }
    
      } else {
       strcpy(Lista->impoUtente.ipUtente, ipUtenteTmp);
       Lista->impoUtente.oraAccesso=oraAccessoTmp;
       Lista->pStesso=NULL;
       verificaUtente=1;
      }
    
      if(!verificaUtente) {
       Lista->pStesso=(impoUtenti*)malloc(sizeof(impoUtenti));
       Lista=Lista->pStesso;
       strcpy(Lista->impoUtente.ipUtente, ipUtenteTmp);
       Lista->impoUtente.oraAccesso=oraAccessoTmp;
       Lista->pStesso=NULL;
      }
    
      fclose(fp);
      Lista=cLista;
      fp=NULL;
      fp=fopen(NOME_FILE, "wb");
    
      while(Lista!=NULL) {
       if(Lista->impoUtente.oraAccesso>(oraAccessoTmp-6000)) {
        fwrite(Lista, sizeof(impoUtenti), 1, fp);
        contaUtenti++;
       }
       Lista=Lista->pStesso;
      }
      fclose(fp);
    
      printf("Utenti connessi: %d", contaUtenti);
    
      Lista=cLista;
      liberaMemoria(Lista);
      system("pause");
      return 0;
    }
    
    void liberaMemoria(impoUtenti* pTmp) {
     impoUtenti* tLista=NULL;
    
     while(pTmp!=NULL) {
      tLista=pTmp->pStesso;
      free(pTmp);
      pTmp=tLista;
     }
     return;
    }
    C/C++

  2. #2
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    up
    C/C++

  3. #3
    Se non dici qual e' il problema...
    Folle e' l'uomo che parla alla luna.
    Stolto chi non le presta ascolto.

  4. #4
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    ...

    ieri l'ho collaudato un po, anche se il file è vuoto mi riempe una lista....ma non penso che sia questo il problema
    C/C++

  5. #5
    Utente di HTML.it L'avatar di C232
    Registrato dal
    Dec 2004
    Messaggi
    303

    Ok, ho rifatto il codice... Ora va tutto bene...

    Posto, perche potrebbe servire...

    codice:
    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    #include <time.h>
    
    #define NOME_FILE "uc.bin"
    #define ERRORE_USCITA "C\'è un solo utente connesso"
    
    int verificaIp(char* IPTmp);
    
    struct strDati {
      char ipUtente[80];
      int oraAccesso;
    };
    
    typedef struct strContDati {
     struct strDati impoUtente;
     struct strContDati* pStesso;
    } impoUtenti;
    
    void liberaMemoria(impoUtenti*);
    
    FILE* fp=NULL;
    int contaUtenti=0;
    int verificaUtente=0;
    
    int main(int argc, char *argv[]) {
    
      char* ipUtenteTmp=getenv("REMOTE_ADDR"); // "127.0.0.1";
      int oraAccessoTmp=time('\0');
    
      impoUtenti* Lista=(impoUtenti*)malloc(sizeof(impoUtenti));
      impoUtenti* cLista=Lista;
    
      printf("content-type:text/html\n\n");
      if(!ipUtenteTmp) {
       printf("%s", ERRORE_USCITA);
       free(Lista);
       exit(1);
      }
    
      fp=fopen(NOME_FILE, "rb");
    
      if(fp==NULL) {
       printf("%s", ERRORE_USCITA);
       free(Lista);
       exit(1);
      }
    
      while(!feof(fp)) {
    
       fread(Lista, sizeof(impoUtenti), 1, fp);
    
       if(verificaIp(Lista->impoUtente.ipUtente)) {
        strcpy(Lista->impoUtente.ipUtente, Lista->impoUtente.ipUtente);
        if(!strcmp(Lista->impoUtente.ipUtente, ipUtenteTmp)) {
         Lista->impoUtente.oraAccesso=oraAccessoTmp;
         verificaUtente=1;
        }
        else {
         Lista->impoUtente.oraAccesso=Lista->impoUtente.oraAccesso;
        }
    
    
        if(!feof(fp)) {
         Lista->pStesso=(impoUtenti*)malloc(sizeof(impoUtenti));
         Lista=Lista->pStesso;
        }
        else {
         Lista->pStesso=NULL;
        }
       }
       else {
        Lista->pStesso=NULL;
       }
    
      }
    
      fclose(fp);
    
      if(!verificaUtente) {
       strcpy(Lista->impoUtente.ipUtente, ipUtenteTmp);
       Lista->impoUtente.oraAccesso=oraAccessoTmp;
       Lista->pStesso=NULL;
      }
    
      Lista=cLista;
      fp=NULL;
      fp=fopen(NOME_FILE, "wb");
    
    
      while(Lista!=NULL) {
       if(Lista->impoUtente.oraAccesso>(oraAccessoTmp-600)&& verificaIp(Lista->impoUtente.ipUtente)) {
        fwrite(Lista, sizeof(impoUtenti), 1, fp);
        contaUtenti++;
       }
       Lista=Lista->pStesso;
      }
    
      fclose(fp);
    
      if(contaUtenti>1) {
       printf("Utenti connessi %d", contaUtenti);
      }
      else {
       printf("C\'è un solo utente connesso");
      }
    
      Lista=cLista;
      liberaMemoria(Lista);
      // system("pause");
      return 0;
    }
    
    // verifica ip
    int verificaIp(char* IPTmp) {
      int contaPunti=0;
      int x=0;
    
      for(x=0; IPTmp[x]; x++) {
       if(IPTmp[x]=='.') {
        contaPunti++;
       }
      }
    
      if(contaPunti==3) {
       return 1;
      }
      else {
       return 0;
      }
    }
    
    // libera memoria
    void liberaMemoria(impoUtenti* pTmp) {
     impoUtenti* tLista;
    
     while(pTmp!=NULL) {
      tLista=pTmp->pStesso;
      free(pTmp);
      pTmp=tLista;
     }
    
    }
    C/C++

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.