Bonjour,

J'ai écris un client serveur en C, envoyant un message en json (librairie json-c : https://github.com/json-c/json-c ).
La connexion s'effectue bien entre le client et le serveur, le client envoyé bien un message (retour de send : 167), mais le serveur ne reçoit rien (retour de recv = -1 ). J'ai essayé de voir si l'erreur venait d'ailleurs, mais ça n'a pas l'air d'être le cas (au vue des retours que j'ai).


Voici mon code :
client
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
 
#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 100
 
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");
        Sleep(5000);
        return EXIT_FAILURE;
    }
    int r = 0;
    r = send(fd, temp_buff, strlen(temp_buff),0);
    if (r == -1) //checke
    {
        perror("write");
        Sleep(5000);
        return EXIT_FAILURE;
    }
    printf("nb caractere envoyé : %i",r);
    printf("Written data\n");
    Sleep(2000);
    WSACleanup();
    return EXIT_SUCCESS;
}
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
 
#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;
    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];
    printf("Max size : %i \n", MAX_SIZE); // ok
    Sleep(3000);
    for (;;)
    {
        printf("%c \n", buff);
        r = recv(connfd, buff, MAX_SIZE,0);
        Sleep(500);
        printf("longueur buff : %i \n",r);
        buff[r]=0;
        printf("%c \n",buff);
        if (r == -1)
        {
            perror("read");
            printf("1 \n");
            Sleep(2000);
            return EXIT_FAILURE;
        }
        if (r == 0)
            printf("2 \n");
            break;
 
        printf("READ: %s\n", buff);
    }
    Sleep(3000);
    WSACleanup();
    return EXIT_SUCCESS;
}
Est ce que quelqu'un aurait déjà été confronté à ce problème ou serait comment le résoudre?
Merci d'avance