IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Réseau C Discussion :

Aide pour la création d'un serveur


Sujet :

Réseau C

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 5
    Points : 2
    Points
    2
    Par défaut Aide pour la création d'un serveur
    Bonjour à tous, je suis actuellement entrain de créer un serveur à l'aide d'une fonction de hashage qui va enregistrer les clés et valeurs d'une personne sur le serveur pour ensuite l'enregistrer sur un journal. En ce moment, tout est bon au niveau du hashage. Mon seul problème est la manipulation du server (dans le code). Mon problème est donc le suivant: je pense pouvoir gérer les erreurs (n'importe quel type d'erreur: connexion perdue etc...). Mais je ne comprend pas pourquoi cela ne fonctionne pas. Je ne sais pas si je manipule mal mes fonctions mais voici mon code (uniquement le code dédié au serveur):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    #include <sys/select.h>
    #include <assert.h>
    #include <signal.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
     
    //#include "common.h"
    #include "store.h"
     
    #define BUF_LEN ( 6 + MAX_KEY_LEN + MAX_VALUE_LEN + 1 )
    #define BLANK(buf,len) memset( (buf), 0, (len) )
     
    /* A connection was accepted, waiting for the command */
    #define READING 0
    /* Received the command, must send the answer */
    #define WRITING 1
    /* Sending the result (WRITING should be true) */
    #define RESULT  2
    /* Sending the result (WRITING should be true) */
    #define ERROR   4
    /* The connection can be closed */
    #define DONE    8
     
     
    //===========================//
    //file descriptor state management.
    //remember, INDEX of the FDState 
    //container is the magic word.
    typedef struct {
      char buffer[BUF_LEN];
      int end;             
      int cur;             
      int status;
    } FDState;
     
    void resetState( FDState *state ) {
    //initialize state.
        FD_ZERO(&state);
        state = READING;
    //default status is READING
    }
     
    FDState *allocFDState() {
        FDState *state;
        state = malloc(sizeof(FDState));
        //allocate wrapper
      return state;
    }
     
    void freeFDState(FDState *state) {
        free(state);
      //clean up wrapper
    }
     
    FDState *states[FD_SETSIZE]; //state container as described in TP7 giveaway
    //===========================//
     
     
     
     
    int listener; //the main listener file descriptor for the server socket - non blocking property for all connections derived from this one after you set it.
    Store *store;
     
    void setNonBlocking(int fd) {
     
        int flags = fcntl(fd, F_GETFL, 0); 						//Prend les flags utilisés par fd
     
        if(fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0){ 				//Set les anciens flags et les flags non bloquant
            if(errno == EAGAIN || errno == EWOULDBLOCK)
                fprintf(stderr, "Socket : Toutes les sockets sont utiliséss\n");  	//Affiche si il y a une erreur
        }
    }
    int receiveCommand(int fd, FDState *state)
    {
        char buffer[BUF_LEN];
        ssize_t nread;
        while(1) {
            nread = read( fd, buffer, BUF_LEN );
            if( nread < 0 ) {
                perror( "Reading fifo failed ");
                exit(EXIT_FAILURE);
            }
        }
            return 0;
    	//read from socket here
    	// return 0 for success
    	//return -1 for failure
    }
     
    int sendResult( int fd, FDState *state, Store *store )
    {
        char buffer[BUF_LEN];
        ssize_t nread, nwrit;
        nwrit = write( STDOUT_FILENO, buffer, nread );
        if( nwrit != nread ) {
            perror( "Writing to STDOUT failed" );
            exit(EXIT_FAILURE);
        }
            return 0;
    	//write to socket here
    	// return 0 for success
    	//return -1 for failure
    }
     
     
    void run( int port )
    {
        struct sockaddr_in serveur;
        listener = socket(AF_INET, SOCK_STREAM, 0);
        serveur.sin_family = AF_INET;
        serveur.sin_addr.s_addr = htonl(INADDR_ANY);
        serveur.sin_port = htons(port);
        ssize_t nread;                                  //
     
     
        if (bind(listener, (struct sockaddr *) &serveur, sizeof(serveur)) < 0) {
            printf(strerror(errno));
            exit(EXIT_FAILURE);
        }
        listen(listener, 3);
        while (1) {
            char client_message[2000];
            char tmp_message[200];
            int read_size;
            strcpy(tmp_message, "Message recieved");
            struct sockaddr_in client;
            unsigned int clientLength = sizeof(client);
            int clientSock = accept(listener, (struct sockaddr *) &client, &clientLength);
            printf("\nConnexion with success\n");
            read_size = recv(clientSock, client_message, strlen(client_message), 0);
            write(clientSock, tmp_message, strlen(tmp_message));
            if(read_size == 0) {
                printf("\nClient disconnected\n");
            }
            if( nread == 0 ) {                              //
                printf( "Received End-Of-File\n" );
                break;
            }
            else if(read_size = -1) {
                exit(EXIT_FAILURE);
            }
        }
     
    //here is where the magic happens.
    //check the TP7 "inside your run_server function
    //handle ALL ERRORS
    }
     
    void shutdownServer( int signum )
    {
        resetState(states);
        freeFDState(*states);
    	//signal callback
    	//do your clean-up here.
    	exit(EXIT_SUCCESS);
    }
     
    int main(int argc, char **argv)
    {
      int port = 9998;
      if( argc == 2 )
      {
    	  port = atoi( argv[1] );
      }
     
        Store *store = NULL;
        store = openStore("journal.txt");
        run(port);
      //1) prepare sigaction and callback
      //2) open the store
      //3) run the server
    }

  2. #2
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Développeur en systèmes embarqués
    Inscrit en
    Septembre 2007
    Messages
    7 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 369
    Points : 23 623
    Points
    23 623
    Par défaut
    Bonjour,

    Qu'est-ce qui ne fonctionne pas ?

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Je viens d'avancer mon code drastiquement, je sens être sur la bonne direction personne toujours pour m'aider ?


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    #include <sys/select.h>
    #include <assert.h>
    #include <signal.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
     
    //#include "common.h"
    #include "store.h"
     
    #define BUF_LEN ( 6 + MAX_KEY_LEN + MAX_VALUE_LEN + 1 )
    #define BLANK(buf,len) memset( (buf), 0, (len) )
     
    /* A connection was accepted, waiting for the command */
    #define READING 0
    /* Received the command, must send the answer */
    #define WRITING 1
    /* Sending the result (WRITING should be true) */
    #define RESULT  2
    /* Sending the result (WRITING should be true) */
    #define ERROR   4
    /* The connection can be closed */
    #define DONE    8
     
     
    //===========================//
    //file descriptor state management.
    //remember, INDEX of the FDState 
    //container is the magic word.
    typedef struct {
      char buffer[BUF_LEN];
      int end;             
      int cur;             
      int status;
    } FDState;
     
    void resetState( FDState *state ) {
    //initialize state.
        FD_ZERO(&state);
        state = READING;
    //default status is READING
    }
     
    FDState *allocFDState() {
        FDState *state;
        state = malloc(sizeof(FDState));
        //allocate wrapper
      return state;
    }
     
    void freeFDState(FDState *state) {
        free(state);
      //clean up wrapper
    }
     
    FDState *states[FD_SETSIZE]; //state container as described in TP7 giveaway
    //===========================//
     
     
     
     
    int listener; //the main listener file descriptor for the server socket - non blocking property for all connections derived from this one after you set it.
    Store *store;
     
    void setNonBlocking(int fd) {
     
        int flags = fcntl(fd, F_GETFL, 0); 						//Prend les flags utilisés par fd
     
        if(fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0){ 				//Set les anciens flags et les flags non bloquant
            if(errno == EAGAIN || errno == EWOULDBLOCK)
                fprintf(stderr, "Socket : Toutes les sockets sont utiliséss\n");  	//Affiche si il y a une erreur
        }
    }
    int receiveCommand(int fd, FDState *state)
    {
        char buffer[BUF_LEN];
        ssize_t nread;
        while(1) {
            nread = read( fd, buffer, BUF_LEN );
            if( nread < 0 ) {
                perror( "Reading fifo failed ");
                exit(EXIT_FAILURE);
            }
        }
            return 0;
    	//read from socket here
    	// return 0 for success
    	//return -1 for failure
    }
     
    int sendResult( int fd, FDState *state, Store *store )
    {
        char buffer[BUF_LEN];
        ssize_t nread, nwrit;
        nwrit = write( STDOUT_FILENO, buffer, nread );
        if( nwrit != nread ) {
            perror( "Writing to STDOUT failed" );
            exit(EXIT_FAILURE);
        }
            return 0;
    	//write to socket here
    	// return 0 for success
    	//return -1 for failure
    }
     
     
    void run( int port )
    {
        struct sockaddr_in serveur;
        listener = socket(AF_INET, SOCK_STREAM, 0);
        serveur.sin_family = AF_INET;
        serveur.sin_addr.s_addr = htonl(INADDR_ANY);
        serveur.sin_port = htons(port);
     
     
        if (bind(listener, (struct sockaddr *) &serveur, sizeof(serveur)) < 0) {
            printf(strerror(errno));
            exit(EXIT_FAILURE);
        }
        listen(listener, 3);
        while (1) {
            setNonBlocking(listener);
            FDState *allocFDState();
            receiveCommand(listener, states);
            states == WRITING;
            sendResult(listener, states, store);
        }
    //here is where the magic happens.
    //check the TP7 "inside your run_server function
    //handle ALL ERRORS
    }
     
    void shutdownServer( int signum )
    {
        resetState(states);
        freeFDState(*states);
    	//signal callback
    	//do your clean-up here.
    	exit(EXIT_SUCCESS);
    }
     
    int main(int argc, char **argv)
    {
      int port = 9998;
      if( argc == 2 )
      {
    	  port = atoi( argv[1] );
      }
     
     
        store = openStore("journal.txt");
        run(port);
        shutdownServer(port);
      //1) prepare sigaction and callback
      //2) open the store
      //3) run the server
    }

  4. #4
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par Obsidian Voir le message
    Bonjour,

    Qu'est-ce qui ne fonctionne pas ?
    J'ai pas l'impression que dans le code les fonctions sont bien définies et bien utilisées.

  5. #5
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 859
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 859
    Points : 218 580
    Points
    218 580
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    Pour vérifier, on peut soit : rajouter des printf(), soit utiliser le débogueur.
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

Discussions similaires

  1. Aide pour la création d'un serveur BBS
    Par Fracasse dans le forum Réseau
    Réponses: 2
    Dernier message: 28/03/2013, 13h01
  2. Réponses: 1
    Dernier message: 17/05/2006, 15h27
  3. Réponses: 2
    Dernier message: 10/03/2006, 13h55
  4. [Oracle] Aide pour la création d'un trigger
    Par Sonic dans le forum Administration
    Réponses: 14
    Dernier message: 04/11/2004, 19h54
  5. [MATOS]Aide pour le choix d'un serveur...
    Par hpalpha dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 17/09/2004, 21h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo