Partage
  • Partager sur Facebook
  • Partager sur Twitter

client serveur a cessé de fonctionner.

Sujet résolu
    28 mars 2017 à 11:58:46

    Bonjour,

    Je fais un programme client serveur sous windows en localhost. Le client envoie des données en json (une librairie est inclut pour ca) et le serveur s'occupe juste de recevoir et lire ces données.

    Jusqu'à la tout se passe bien, les données sont bien reçu et lu. Mais à la fin du programme, après que j'ai fermé les sockets j'ai le programme qui plante : serveur.exe a cessé de fonctionner ... 
    J'ai chercher et je ne vois pas ou est l'erreur, quelqu'un a une idée?
    Voici le code:
    Serveur

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include "json.h"
    #include <sys/types.h>
    #include <winsock2.h>
    #include <winsock.h>
    
    #pragma comment(lib, "ws2_32.lib")
    
    #define MAX_SIZE 500
    
    int main()
    {
        //obligatoire pour windows
        WSADATA wsaData;
       	WSAStartup(MAKEWORD(2, 2), &wsaData);
    
        int listenfd = 0, connfd = 0;   //related with the server
        struct sockaddr_in serv_addr;
        json_object *jobj = json_object_new_object(); //Pour récupérer la string et la mettre dans un json
    
        //json_object * jobj;
        uint8_t buf[158], i;
    
        memset(&buf, '0', sizeof(buf));
    
        //Création socket
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if(listenfd == -1)
        {
            printf("Probleme creation socket \n");
        }
        else
        {
            puts("Socket cree");
        }
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        serv_addr.sin_port = htons(8888);
    
        if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
        {
            printf("Bind error \n");
        }
        else
        {
                puts("bind fait");
        }
    
        //Listen
        listen(listenfd, 5);
        printf("listening\n");
        printf("connfd : %i \n",connfd);
    
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
        if(connfd == SOCKET_ERROR)
        {
            perror("accept()");
        }
        printf("connfd : %i \n",connfd); //ok
        printf("Reading from client\n");
    
        int r;
    
        char buff[MAX_SIZE] = {0};
        printf("Max size : %i \n", MAX_SIZE); // ok
    
    
        r = recv(connfd, buff, MAX_SIZE,0);
        printf("longueur buff : %i \n",r);
    
        if (r == -1)
        {
            perror("read");
            return EXIT_FAILURE;
        }
    
        if (strcpy(json_object_to_json_string(jobj), buff) == NULL) //on met le buff dans un json
        {
            perror("strcpy");
            return EXIT_FAILURE;
        }
        printf("buff : %s \n", buff);
      
        Sleep(1000);
        printf("1 \n");
        closesocket(listenfd);
        printf("2 \n");
        WSACleanup();
        printf("3 \n");
        return EXIT_SUCCESS;
    }
    
    

    Client:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <winsock2.h>
    #include <winsock.h>
    #include "json.h"
    
    #pragma comment(lib, "ws2_32.lib")
    
    #define MAX_SIZE 500
    
    int main()
    {
        //Obligatoire pour Windows
        WSADATA wsaData;
       	WSAStartup(MAKEWORD(2, 2), &wsaData);
    
    
        char* str;
        int fd = 0;
        struct sockaddr_in demoserverAddr;
    
        fd = socket(AF_INET, SOCK_STREAM, 0);
    
        if (fd < 0)
        {
            printf("Error : Could not create socket\n");
            Sleep(1000);
            return 1;
        }
        else
        {
            demoserverAddr.sin_family = AF_INET;
            demoserverAddr.sin_port = htons(8888); //a modifier pour correspondre au bonne adresse
            demoserverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //idem
            memset(demoserverAddr.sin_zero, '\0', sizeof(demoserverAddr.sin_zero));
        }
    
        if (connect(fd, (const struct sockaddr *)&demoserverAddr, sizeof(demoserverAddr)) < 0)
        {
            printf("ERROR connecting to server\n");
            Sleep(1000);
            return 1;
        }
    
        /*Creating a json object*/
        json_object *jobj = json_object_new_object();
    
        /*Creating a json string*/
        json_object *jstring = json_object_new_string("Joys of Programming");
    
        /*Creating a json integer*/
        json_object *jint = json_object_new_int(10);
    
        /*Creating a json boolean*/
        json_object *jboolean = json_object_new_boolean(1);
    
        /*Creating a json double*/
        json_object *jdouble = json_object_new_double(2.14);
    
        /*Creating a json array*/
        json_object *jarray = json_object_new_array();
    
        /*Creating json strings*/
        json_object *jstring1 = json_object_new_string("c");
        json_object *jstring2 = json_object_new_string("c++");
        json_object *jstring3 = json_object_new_string("php");
    
        /*Adding the above created json strings to the array*/
        json_object_array_add(jarray,jstring1);
        json_object_array_add(jarray,jstring2);
        json_object_array_add(jarray,jstring3);
    
        /*Form the json object*/
        /*Each of these is like a key value pair*/
        json_object_object_add(jobj,"Site Name", jstring);
        json_object_object_add(jobj,"Technical blog", jboolean);
        json_object_object_add(jobj,"Average posts per day", jdouble);
        json_object_object_add(jobj,"Number of posts", jint);
        json_object_object_add(jobj,"Categories", jarray);
    
        printf("Size of JSON object- %lu\n", sizeof(jobj));
        printf("Size of JSON_TO_STRING- %lu,\n %s\n", sizeof(json_object_to_json_string(jobj)), json_object_to_json_string(jobj));
        //printf("Size of string- %lu\n", sizeof(json_object_to_json_string(jobj)));
    
        char temp_buff[MAX_SIZE];
    
        if (strcpy(temp_buff, json_object_to_json_string(jobj)) == NULL)
        {
            perror("strcpy");
            return EXIT_FAILURE;
        }
        int r = 0;
        r = send(fd, temp_buff, strlen(temp_buff),0);
        if (r == -1) //check
        {
            perror("write");
            return EXIT_FAILURE;
        }
        printf("nb caractere envoyé : %i",r);
        printf("Written data\n");
        Sleep(3000);
        closesocket(fd);
        WSACleanup();
        return EXIT_SUCCESS;
    }
    

    Merci d'avance :)

     

    EDIT : 
    Problème résolu, j'ai revue et restructuré le code plus proprement.  

    -
    Edité par dreadjunk 28 mars 2017 à 15:14:20

    • Partager sur Facebook
    • Partager sur Twitter

    client serveur a cessé de fonctionner.

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown