【发布时间】:2019-03-17 13:01:50
【问题描述】:
我正在尝试通过套接字从客户端(Python)向服务器(C++)发送布尔值。
所以我使用了 json 并将布尔值转换为字符串格式并通过套接字发送字符串。
这是我的代码: 客户端(Python):
# 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"
data= json.dumps("{\"A\":true,\"B\":false,\"C\":false,\"D\":false}")
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
现在我以字符串格式和 \n \t 标签一起接收它们。 如何解释发送的布尔值并将它们分配给 C++ 中所需的变量。
这是我在服务器端 (C++) 的代码:
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;
}
我是初学者,感谢各位帮助我。
仅供参考 - 如果我在收到字符串后在 C++ 中打印它看起来像这样
"{\"A\":true,\"B\":false,\"C\":false,\"D\":false}"
感谢大家的建议,我尝试在 C++ 中使用 json 并进行了一些更改
#include<iostream>
#include <jsoncpp/json/json.h>
using namespace std;
void decode()
{
bool a,b,c,d;
string text = "{\"A\":true,\"B\":false,\"C\":false,\"D\":false}";
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse( text, root );
if ( !parsingSuccessful )
{
cout << "Error parsing the string" << endl;
}
const Json::Value mynamesA = root["A"];
const Json::Value mynamesB = root["B"];
const Json::Value mynamesC = root["C"];
const Json::Value mynamesD= root["D"];
cout<<mynamesA<<endl;
cout<<mynamesB<<endl;
cout<<mynamesC<<endl;
cout<<mynamesD<<endl;
}
int main()
{
decode();
return 0;
}
现在开始打印了
true
false
false
false
正如预期的那样。
但我想将这些值分配给bool a,b,c,d;
我该怎么做??
【问题讨论】:
-
看看this JSON library for C++
标签: python c++ json sockets deserialization