I want to send data to JSON through sockets on a client server application written in C.
I am using json-c / libjson to process JSON data in a C application.
While working on some tutorials, I can create a JSON object and parse it successfully.
Now I want to use the JSON data format for server-client communication.
Here is part of my server and client code
server.c
int main()
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
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("%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;
}
json_object *jobj = json_object_new_object();
json_object *jstring = json_object_new_string("Joys of Programming");
json_object *jint = json_object_new_int(10);
json_object *jboolean = json_object_new_boolean(1);
json_object *jdouble = json_object_new_double(2.14);
json_object *jarray = json_object_new_array();
json_object *jstring1 = json_object_new_string("c");
json_object *jstring2 = json_object_new_string("c++");
json_object *jstring3 = json_object_new_string("php");
json_object_array_add(jarray,jstring1);
json_object_array_add(jarray,jstring2);
json_object_array_add(jarray,jstring3);
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));
write(fd, json_object_to_json_string(jobj), 157);
printf("Written data\n");
return 0;
}
I want to send json_object jobj from client to server. How to do it?
Some things I tried:
When I use write(fd, jobj, sizeof(jobj)), the client sends only 8 bytes, and other characters are null when I receive data on the server.
json_object jobj, write(fd, json_object_to_json_string(jobj), 157), 157 - jobj.
, read(connfd, jobj, sizeof(jobj)), 8 ( write(fd, jobj, sizeof(jobj))).
server.c, JSON ( ). ( ).
json_object jobj ?
json_object jobj ?