S
S
SKEPTIC2019-03-28 19:17:57
Python
SKEPTIC, 2019-03-28 19:17:57

Client Server C++ Python?

I'm learning C++ and playing around with Python at the same time, so I decided to write a simple console chat.
The C++ application will act as the client, and the script will act as the Python server.
The problem is that sending a message from C++ to Python seems to cause no problems, only the Cyrillic alphabet is not sent (in Python it gives out some Chinese characters).
But sending a message from Python to C++ causes one problem.
I get a message in the C ++ console and at the end some incomprehensible gibberish, I understand that there may be some problem with the encoding, but the main thing is that the message displays everything (naturally, only numbers and Latin), and at the end, whatever one may say, some symbols.
Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SKEPTIC, 2019-03-29
@pro100chel

Solution:
In python, encode the message with Windows1251 encoding

data = 'Какое-то сообщение'
data.encode('cp1251')

In C++, organize support for encoding when outputting to the console using 2 lines of code:
SetConsoleCP(1251);
SetConsoleOutputCP(1251);

Also in C++ it is necessary after creating the array where the message is received:
char message[256]; //Создание массива для сообщения
std::memset(message, 0, sizeof message); //очистка от мусора
recv(ConnectServer, message, sizeof(message), NULL); //принятие сообщения
std::cout << message << std::endl; //вывод сообщения на экран

That's the whole secret.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question