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
}