Bonjour,
J'ai fait un programme qui fait le transfert d'une fichier JSON en utilisant socket du client vers le serveur. En fait, je voulais envoyer un message après le recevoir du fichier du serveur vers le client mais apparemment il y a une problème qui cloche, donc le message n'arrive pas au client. Pouvez-vous m'aider SVP. Voici les codes sources:
Serveur.c
client.cCode:#include <ctype.h> #include <errno.h> #include <fcntl.h> #include <json-c/json.h> #include <netinet/in.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/socket.h> #include <unistd.h> #include "jsmn.h" #define BUFFER_SIZE 1024 #define MARGE_PROGRESSION 100 #define BUFFER_SIZE_JSON 100000 #define MAX_TOKEN_COUNT 128 pthread_t th_server; pthread_cond_t cond_sr; pthread_mutex_t verrou_sr; /*********************************************************************** * Main.c: **********************************************************************/ int main(int argc, char *argv[]) { FILE* fichier_sr; int socket = 0; int size_line, desc_write; long fichier_size = 0, size_avanc_sr = 0; char* fichier_sr_nom = "server_test.json"; char* line_buf = (char *) malloc (BUFFER_SIZE*sizeof(char)); char* Msg_to_cl = (char* )malloc(BUFFER_SIZE*sizeof(char)); Msg_to_cl = "hello from server\n"; pthread_mutex_init (&verrou_sr, NULL); pthread_cond_init (&cond_sr, NULL); fichier_sr = fopen(fichier_sr_nom,"w"); socket = attente (socket, argv[1]); fichier_size = lire_taille_fich(socket, fichier_size); pthread_create(&th_server,NULL,thread_pourcentage_server,NULL); while((size_line = recevoir_fichier(size_line,socket, line_buf)) >= 0) { if (size_line == 0) { break; } desc_write = ecrire_fichier(desc_write,size_line,fichier_sr,line_buf); size_avanc_sr += size_line; pthread_mutex_lock(&verrou_sr); result_server=100*((float)(size_avanc_sr)/(float)(fichier_size)); pthread_mutex_unlock(&verrou_sr); pthread_cond_broadcast(&cond_sr); usleep(100000); if (desc_write < size_line) { perror("File write failed on server.\n"); } } pthread_join(th_server, NULL); pthread_mutex_destroy (&verrou_sr); pthread_cond_destroy (&cond_sr); fclose(fichier_sr); //parseJSON(fichier_sr_nom); send(socket,Msg_to_cl,BUFFER_SIZE,0); return 0; }
Merci d'avanceCode:#include <arpa/inet.h> #include <stdio.h> #include <unistd.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include <sys/stat.h> #include <pthread.h> #define BUFFER_SIZE 1024 #define MARGE_PROGRESSION 100 pthread_t th_client; pthread_cond_t cond_cl; pthread_mutex_t verrou_cl; /*********************************************************************** * Main.c: **********************************************************************/ int main(int argc, char *argv[]) { FILE* fichier_cl; int socket = 0; long size_file = 0, size_avanc_cl = 0; char* line_str = NULL; char* file_name = "client_file.json"; char* Msg_from_sr = (char* )malloc(BUFFER_SIZE*sizeof(char)); size_t line_pos = 0; ssize_t desc_line; pthread_mutex_init (&verrou_cl, NULL); pthread_cond_init (&cond_cl, NULL); fichier_cl = fopen(file_name,"r"); size_file = taille_fich(file_name); socket = connecte(socket,argv[1],argv[2]); socket = envoyer_taille_fich(socket,size_file); pthread_create(&th_client,NULL,thread_pourcentage_client,NULL); while ((desc_line = getline(&line_str, &line_pos, fichier_cl)) != -1) { if((socket = envoyer_fich(socket,line_str)) < 0) { printf("ERROR: Failed to send file %s.\n", file_name); break; } size_avanc_cl += strlen(line_str); pthread_mutex_lock(&verrou_cl); result_thread=100*((float)(size_avanc_cl)/(float)(size_file)); pthread_mutex_unlock(&verrou_cl); pthread_cond_broadcast(&cond_cl); //usleep(100000); if (line_str) { free(line_str); } line_pos = 0; } pthread_join(th_client, NULL); pthread_mutex_destroy (&verrou_cl); pthread_cond_destroy (&cond_cl); fclose(fichier_cl); read(socket, Msg_from_sr, 100); return 0; }
-----