How can I send a Java string with Unicode characters to C++ via socket, without strange characters? -


i working on application android phone can import sms, read them , reply sms. worked planned when programming server , client. if had problems, google search gave me solutions, time, first time of life, asking help.

the problem:

the problem is, when client(java) sends sms content contains unicode characters such "å, ä, ö", c++ cannot read them.

my program works sends packet size first make other aware of how big packet come. e.g java calculates packet 121 bytes , sends server. if packet contains few non ansi characters, c++ not receive 121 bytes, 123 bytes, , non-ansi chars become strange.

i've been googling day without answers. i've tried wchar_t in c++, i've tried set in java sent using utf-8, i've been debugging hours recreate problem , try different things, without success!

so going on here? how can text java c++ in correct size , representation in java? packets without unicode chars works fine.

thank guys! little tired atm, hope didn't miss anything. code little messy, prototype yet.

p:s, tcp conenction.

-server c++ recv function-

bool receive( std::string& msg) {     zreadmutex.lock();      try     {         int errcode;         unsigned int packetsize = 0;         char packetsizebuffer[4];          //get packet size         errcode = recv(zsocket, packetsizebuffer, sizeof(packetsizebuffer), 0);          if ( errcode == socket_error || errcode == 0)         {             throw networkexception("failed receiving packet size!", wsagetlasterror());         }          //convert         packetsize = chararraytounsignedint(packetsizebuffer);          if (packetsize == 0)         {             throw networkexception("connection closed!");         }          //calculate chunks          //total bits received         unsigned int totalbits = 0;         //calculate number of chunks arrive         int chunks = caculatechunks(packetsize);         //counter chunk loop         int count = 0;         //add message every chunk received         std::string message = "";          //just temp check         if (chunks > 15)         {             throw networkexception("connection closed!");         }          //get chunks         while (count < chunks)         {             char* buffer = new char[zmaxchunksize];              if ((errcode = recv(zsocket, buffer, zmaxchunksize, 0)) <= 0)             {                 if (errcode < 0)                 {                     delete [] buffer;                     throw networkexception("failed receiving packet data!", wsagetlasterror());                 }                 else                 {                     delete [] buffer;                     throw networkexception("connection closed!");                 }              }              totalbits += errcode;             count++;             message += buffer;              delete [] buffer;          }          if (packetsize != totalbits)         {             throw networkexception("message not expected size!");         }          message.resize(totalbits);         msg = std::string(message);      }     catch(...)     {         zreadmutex.unlock();         throw;     }      zreadmutex.unlock();     return true; } 

- client java send function -

public boolean initsender() {     if(msocket == null)         return false;      try {         //auto flush false, auto flush anyways         out = new printstream(msocket.getoutputstream(), false, "utf-8");      } catch (ioexception e) {         e.printstacktrace();         return false;     }      return true; }  public synchronized void sendmessage(final string a) {     int size = 0;     size = a.length();      //send size     out.print(size);      //chunk     int chunks = calculatechunks(a);     string[] data = splittochunks(a, chunks);      (string message : data)     {         //send data         out.print(message);     } } 

so e.g java calculates packet 121 bytes , sends server.

size = a.length(); //send size out.print(size); 

that code doesn't match description; .length() on java string doesn't count bytes. you're sending number of java char elements in string. java char 2 bytes.

        out.print(message); 

message java string. need @ how string gets converted bytes sent across network connection. there's no guarantee conversion creates same number of bytes there java chars in string. in particular, if string converted utf-8 individual java char values converted 2 or 3 bytes.

you need conversion before sending data can count actual number of bytes being sent.


on c++ side, std::string sequence of c++ char elements, aren't same java chars. c++ char single byte. in code std::string contain same data read off network; if client sends utf-8 data, std::string holds utf-8 data. display string you'll need use api handles whatever encoding is, or convert it. otherwise some of characters 'strange'.


here's reasonable start on learning of things need know:

the absolute minimum every software developer absolutely, positively must know unicode , character sets (no excuses!)


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -