我在 python 中的服务器:
import time
import zmq
context = zmq.Context()
socket = context.socket( zmq.REP )
socket.bind( "tcp://*:5555" )
while True:
# Wait for next request from client
message = socket.recv()
print( "Received request: %s" % message )
# Do some 'work'
time.sleep( 1 )
# Send reply back to client
socket.send( b"World" )
我的 C 客户端:
std::string str = std::to_string(bar.open) + ';'
+ std::to_string(bar.high) + ';'
+ std::to_string(bar.low) + ';'
+ std::to_string(bar.close) + ';'
+ std::to_string(bar.volume) + ';'
+ bar.time + ';'
+ std::to_string(bar.hour) + ';'
+ std::to_string(bar.minute) + ';'
+ bar.date + ';'
+ bar.symbol;
void *context = zmq_ctx_new ();
void *requester = zmq_socket ( context, ZMQ_REQ );
zmq_connect ( requester, "tcp://localhost:5555" );
char buffer [10];
printf ( "Sending data to python module\n" );
zmq_send ( requester, static_cast<void*>(&str), 1000, 0 );
zmq_recv ( requester, buffer, 10, 0 );
printf ( "Received %s\n", buffer );
zmq_close ( requester );
当我从 C 客户端发送消息时,python 中打印的数据是乱码,如下所示:
Received request: @c�SxH���C��
%�.075600;C�
%�;C �
%0.075600�C@�
%�`�
%���
%���
%0.075600%���
%���
%����C��
如何在 Python 中解码消息以正确打印出来?