Send string data from symbian to .net
Hi!
I need to send string data with special characters from a Symbian S60 3rd Ed. phone to a server running on a Windows platform. The server is written in C++/CLI and I need the string data as a managed string on the server. I currently convert the string to UTF8 on the phone and send it, like this:
[CODE]
HBufC* heapbuffer = HBufC::NewLC(aMessage.Length());
heapbuffer->Des().Copy( aMessage );
HBufC8* heapbuffer8 = HBufC8::NewLC(aMessage.Length());
TPtr8 tmpBuf8(heapbuffer8->Des());
CnvUtfConverter::ConvertFromUnicodeToUtf8(tmpBuf8,*heapbuffer);
iSocket->Write(*heapbuffer8, iStatus);
[/CODE]
The descriptor aMessage of course contains the message to be sent. Then, on the server I convert it back using the following code:
[CODE]
Encoding^ enc = Encoding::UTF8;
int socket = (int)param, bytesReceived, selectret;
char cbuf[BUFFER_MAX_LENGHT];
bytesReceived = recv(socket, cbuf, BUFFER_MAX_LENGHT, 0);
cbuf[bytesReceived] = '\0';
array<Byte>^ receivedBytes = gcnew array<Byte>(bytesReceived + 1);
for(int i = 0; i < (bytesReceived + 1); i++)
receivedBytes[i] = cbuf[i];
System::String^ managedBuffer = enc->GetString(receivedBytes);
[/CODE]
This works, most of the time. However, sometimes the data seems to be sent incorrectly. For example, I had big problems sending over a string containing the number 26. When appending this number to another string to be passed as an SQL statement to a function, the server crashed. However, if I first convert the string to a number containing the number 26, and the convert it back to a string it works.
Any idea what is wrong with my string conversion? In the end I am simple trying to send data possibly containing special chars to a server running on .NET. How should I do that?
Re: Send string data from symbian to .net
maybe you should base64 the data when sending it over, it should eliminate any problems with binary values of the string items at least.
Re: Send string data from symbian to .net
It worked for me. I had to implement xml-rpc communications between .net server code (c#) and s60 phone, xml part was regular ascii, binary data in the message was base64-ed. Works like a dream.