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;
} |