Answer the question
In order to leave comments, you need to log in
How to set common encoding in client-server application?
I am writing a chat, I am writing a server in c ++, a client application in c #, when a client sends a message in Russian, the server forwards this message, but alas, it sends rubbish (in English everything is fine). How the server should send messages in the same encoding as set it, because it gets confused when receiving a message.
Answer the question
In order to leave comments, you need to log in
Honestly stole on AnswersMail, maybe it will help :)
UTF8 is designed so that characters with codes 0-127 (English letters, numbers, punctuation marks, etc.) occupy one byte, and therefore are displayed correctly, and national language characters occupy two bytes. Hence the abracadabra when displaying Russian text.
You also need to convert everything to Win-1251 single-byte encoding. This can be done in two steps:
- convert what is sent to unicode (for example, using the WinAPI function MultiByteToWideChar, specifying UFT8 as encoding)
- converting the string from unicode to the desired encoding (for example, using the function WideCharToMultiByte, specifying the number 1251 first function parameter)
Alternatively, you can use the functions of the C standard library.
You must work with UNICODE, UTF8. No need to mess with any other encodings.
It all depends on how exactly you transmit this data over the network. Bytes are transmitted over the network, you must be able to interpret them correctly. Examples:
1. Plain text strings, ending with CRLF. You need to encode your UTF via base64.
2. JSON objects. National characters are automatically encoded with esc sequences, this "thickens" the protocol, but is easier to work with and easier to extend. CRLF line endings. The same CRLF inside JSON is also encoded with esc sequences, there will be no jambs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question