Skip to main content

Send and Receive JSON over sockets in server and client application in C

I want to send the data in JSON over sockets in a server-client application written in C.
I am using json-c / libjson library for handling JSON data in C application.
By working on some of the tutorials I am able to create JSON object and able to parse it successfully.
Now I want to use the JSON data format for the communication of server-client.
Here is part of my server and client code
server.c
int main()
{
   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));
   listenfd = socket(AF_INET, SOCK_STREAM, 0);

   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
   serv_addr.sin_port = htons(8888); 

   bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
   printf("binding\n");

   listen(listenfd, 5);
   printf("listening\n");
   connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);

   printf("Reading from client\n");

   while ( (read(connfd, buf, 157)) > 0 )
   {
      for ( i = 0; i < 157; i++ )
         //printf("%s\n", json_object_to_json_string(jobj));
         //json_parse(jobj);
         printf("%d\n", buf[i]);
   }

   return 0;
}
client.c
int main()
{ 
    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");
        return 1;
    }
    else
    {
        demoserverAddr.sin_family = AF_INET;
        demoserverAddr.sin_port = htons(8888);
        demoserverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        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");
         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)));
    write(fd, json_object_to_json_string(jobj), 157);

    printf("Written data\n");
    return 0;
}
I want to send the json_object jobj from client to server. How to do this?
Some things that I tried:
  1. When I use write(fd, jobj, sizeof(jobj)), the client sends only 8 bytes and other characters are null when I receive data on server.
  2. I am only able to send complete json_object jobj when I use write(fd, json_object_to_json_string(jobj), 157) where 157 is the total number of characters in that jobj object.
  3. On server side if I use read(connfd, jobj, sizeof(jobj)) then I only able to receive 8 bytes (on client side I am using write(fd, jobj, sizeof(jobj))).
  4. If I use the above server.c, I am able to receive complete JSON object (if I already know the number of characters in that object). But it is in raw format (receiving byte by byte).
How to send the complete json_object jobj from client side?
And how to receive the complete json_object jobj on server side?

Comments

Popular posts from this blog

Recursive program to insert a star between pair of identical characters

Given a string with repeated characters, we have to insert a star i.e. ” * “  between pair of adjacent identical characters using recursion. Examples: Input : aabb Output : a*ab*b Input : xxxy Output : x*x*xy Recommended: Please try your approach on  {IDE}  first, before moving on to the solution. Approach: If there is an empty string then simply return. This forms our  base condition . Else we do the following- Check if the first two characters are identical. If yes, then insert ” * ” between them. As we have now checked for identical characters at first two positions of the string so we now make a recursive call  without the first character of the string . The above approach has been implemented below: C filter_none edit play_arrow brightness_4 // Recursive CPP program to insert * between // two consecutive same characters. #include <iostream> using namespace std;     // Funct...

Count the number of objects using Static member function

Write a program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class. Explanation:  In this program we are simply explaining the approach of static member function. We can define class members and member functions as static using static keyword. Before understanding static member function, we must understand static member. When we declare a member of a class as static it means no matter how many objects of the class are created, there is  only one copy of the static member . Important points about Static : A static member is shared by all objects of the class, all static data is initialized to zero when the first object is created, if no other initialization is present. A static member function can only access static data member, other static member functions and any other functions from outside the class. By declaring a function member as static, we make it independent of any ...