Receiving strings in C++ from Python (JSON)

I'm trying to send boolean values through sockets from Client(Python) to Server(C++).

So I have used json and converted the boolean values to string format and send the string through sockets.

Here is my code: Client side (Python):

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
# TCP Client Code

host="128.0.0.1"            # Set the server address to variable host

port=8080               # Sets the variable port to 4446

from socket import *             # Imports socket module
import json

s=socket(AF_INET, SOCK_STREAM)      # Creates a socket



s.connect((host,port))          # Connect to server address
print "Successfully connected to the server and ready to send some data"
jsonString = '''
{
    "Styles":["A":true,"B":false,"C":false,"D":false]
}'''
data= json.dumps(jsonString)

s.send(data)



msg=s.recv(1024)            # Receives data upto 1024 bytes and stores in variables msg

print "Message from server : " + msg

s.close()                            # Closes the socket
# End of code


Now I'm receving them in the format of string along with \n \t tags. How can I interpret the boolean values sent and assign them to required variables in C++.

Here is my code on Server side (C++):
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
using namespace std;
//Server side
int main(int argc, char *argv[8080])
{
bool Style1;
    //buffer to send and receive messages with
    char msg[1500];

    //setup a socket and connection tools
    sockaddr_in servAddr;
    memset((char*)&servAddr, 0,sizeof(servAddr));
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = htons(8080);

    //open stream oriented socket with internet address
    //also keep track of the socket descriptor
    int serverSd = socket(AF_INET, SOCK_STREAM, 0);
    if(serverSd < 0)
    {
        cerr << "Error establishing the server socket" << endl;
        exit(0);
    }


    //bind the socket to its local address
    int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr, 
        sizeof(servAddr));
    if(bindStatus < 0)
    {
        cerr << "Error binding socket to local address" << endl;
        exit(0);
    }
    cout << "Waiting for a client to connect..." << endl;


    //listen for up to 5 requests at a time
    listen(serverSd, 5);


    //receive a request from client using accept
    //we need a new address to connect with the client
    sockaddr_in newSockAddr;
    socklen_t newSockAddrSize = sizeof(newSockAddr);


    //accept, create a new socket descriptor to 
    //handle the new connection with client
    int newSd = accept(serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize);
    if(newSd < 0)
    {
        cerr << "Error accepting request from client!" << endl;
        exit(1);
    }
    cout << "Connected with client!" << endl;


        //receive a message from the client (listen)
        cout << "Awaiting client response..." << endl;
        memset(&msg, 0, sizeof(msg));//clear the buffer

        recv(newSd, (char*)&msg, sizeof(msg), 0);
        if(!strcmp(msg, "exit"))
        {
            cout << "Client has quit the session" << endl;

        }
        string str(msg);
        cout << "Client: " << msg << endl;
        cout << ">";
        string data = "Instructions Received \n";
        memset(&msg, 0, sizeof(msg)); //clear the buffer
        strcpy(msg, data.c_str());
        if(data == "exit")
        {
            //send to the client that server has closed the connection
            send(newSd, (char*)&msg, strlen(msg), 0);

        }
        //send the message to client
        send(newSd, (char*)&msg, strlen(msg), 0);

    //we need to close the socket descriptors after we're all done



    close(newSd);
    close(serverSd);

    cout << "Connection closed..." << endl;
    return 0;   
}


I'm a beginner, I appreciate you guys in helping me.

Thanks.
You're receiving some data, and that data is being interpreted as a string, is it? And you're sending the string "true" to indicate boolean true?

1
2
3
4
5
6
7
8
9
bool someValue;
if (receivedString == "true")
{
  someValue = true;
}
else if (receivedString == "false")
{
  someValue = false;
}
Thanks Repeater for the reply.

Regarding your question, Yes, I'm sending the string "true" to indicate boolean value true and send() function in python only sends a string value or buffer through sockets

Topic archived. No new replies allowed.