Go Back   ZeroC Forums > Help Center

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 10-28-2004
ChaosXu ChaosXu is offline
Registered User
 
 
Join Date: Oct 2004
Posts: 2
Question An error occurs when c# client send chinese string to c++ server

Hi:

An error occurs when c# client send chinese string to the c++ server.
The server can receive the string,but the result of unmarshal is wrong,
and an assert appears when release the memory of string.so I browse
the source code of ice and icecs,I see these:

basicstream.cpp:
void
IceInternal::BasicStream::write(const string& v)
{
Int len = static_cast<Int>(v.size());
writeSize(len);
if(len > 0)
{
Container::size_type pos = b.size();
resize(pos + len);
memcpy(&b[pos], v.c_str(), len);
}
}

void
IceInternal::BasicStream::read(string& v)
{
Int len;
readSize(len);
if(b.end() - i < len)
{
throw UnmarshalOutOfBoundsException(__FILE__, __LINE__);
}
if(len > 0)
{
v.assign(reinterpret_cast<const char*>(&(*i)), len);
i += len;
}
else
{
v.clear();
}
}

basicstream.cs:
public virtual void writeString(string v)
{
if(v == null || v.Length == 0)
{
writeSize(0);
return;
}
try
{
byte[] arr = utf8.GetBytes(v);
writeSize(arr.Length);
expand(arr.Length);
_buf.put(arr);
}
catch(Exception)
{
Debug.Assert(false);
}
}

public virtual string readString()
{
int len = readSize();

if(len == 0)
{
return "";
}

try
{
if(_stringBytes == null || len > _stringBytes.Length)
{
_stringBytes = new byte[len];
}
_buf.get(_stringBytes, 0, len);
return utf8.GetString(_stringBytes, 0, len);
}
catch(InvalidOperationException ex)
{
throw new Ice.UnmarshalOutOfBoundsException(ex);
}
catch(System.ArgumentException ex)
{
throw new Ice.MarshalException("Invalid UTF8 string", ex);
}
catch(Exception)
{
Debug.Assert(false);
return "";
}
}

So it seems the implementation of cs support UTF8,and C++ not support UTF8 but MBCS ??
When I use System.Text.Encoding.Default intead of UTF8Encoding in cs client,the server
can receive and print the string correctly.


my platform:
the Ice/IceCS version is 1.5.1, win 2000/vs.net 2003(7.1.3091)/.net 1.1(1.1.4322)

slice:
interface IPrinter
{
void Print(string s);
};

The Server:

class PrinterImpl : public IPrinter
{
public:
PrinterImpl(void);
virtual ~PrinterImpl(void);

virtual void Print(const ::std::string&, const ::Ice::Current& current);
};
PrinterImpl::PrinterImpl(void)
{
}

PrinterImpl::~PrinterImpl(void)
{
}

void PrinterImpl::Print(const ::std::string& s, const ::Ice::Current& current)
{
cout<<s<<endl;
}

Last edited by ChaosXu : 10-28-2004 at 05:30 AM.
Reply With Quote
  #2 (permalink)  
Old 10-28-2004
marc's Avatar
marc marc is offline
ZeroC Staff
 
Name: Marc Laukien
Organization: ZeroC, Inc.
Project: The Internet Communications Engine
 
Join Date: Feb 2003
Location: Florida
Posts: 1,781
C++ also supports UTF-8, it just doesn't check whether every std::string being sent or received is really in UTF-8 format. It is the application's responsibility to make sure that UTF-8 strings are used with Ice. If your application does not use Unicode, you must convert your strings from whatever codeset you use to Unicode first.

Ice for C++ has two utility functions that allow you to convert between a UTF-8 std::string and a UTF-16 std::wstring. Have a look at IceUtil/Unicode.h for details.
Reply With Quote
  #3 (permalink)  
Old 10-29-2004
ChaosXu ChaosXu is offline
Registered User
 
 
Join Date: Oct 2004
Posts: 2
Of couse,string in C# is always in Unicode format,but std::string Ice for C++ use is in ANSI format. UTF8 and ANSI is in the same format only char from 0x00 to 0x7f.MSDN say VC++ 7.x only support MBCS and Unicode.

e.g.
platform Intel x86

string a = "A"
string w = "‰ä" //unicode format: 0x62 0x11

Their bytes in C#:
ansi unicode(C# compiler use) utf8
a [0x41 [0x41 0x00] [0x41]
w [0xce 0xd2] [0x11 0x62] [0xe6 0x88 0x91]

in c++
ansi unicode
a [0x41] [0x41 0x00]
w [0xce 0xd2] [0x11 0x62]

When the C# client send string w,it really send "0xe6 0x88 0x91" because the class Ice.BasicStream of Ice for C# use UTF-8 Encoding(Class System.Text.UTF8Encoding).Then the C++ server receive bytes "0xe6 0x88 0x91" and basicstream of Ice for C++ can't correctly read string from the bytes.It works right if client and server are in the same language.Incorrect if not.

e.g.
send Client Server Result
w C# C# OK
w C++ C++ OK
w C# C++ server error
w C++ C# server error

Last edited by ChaosXu : 10-29-2004 at 12:15 AM.
Reply With Quote
  #4 (permalink)  
Old 10-29-2004
michi's Avatar
michi michi is offline
ZeroC Staff
 
Name: Michi Henning
Organization: ZeroC
Project: Ice
 
Join Date: Feb 2003
Location: Brisbane, Australia
Posts: 912
I think you may have misunderstood Marc. The string that is coming from the C# side is delivered to the C++ as a UTF-8 encoded string. You need to explicitly convert it to a wide string in your C++ application code using the functions defined in Unicode.h.

Cheers,

Michi.
Reply With Quote
  #5 (permalink)  
Old 10-29-2004
marc's Avatar
marc marc is offline
ZeroC Staff
 
Name: Marc Laukien
Organization: ZeroC, Inc.
Project: The Internet Communications Engine
 
Join Date: Feb 2003
Location: Florida
Posts: 1,781
I don't understand what you are exactly asking, i.e., what you expect Ice to do. BasicStream in Ice for C++ doesn't touch the strings it receives, so it doesn't convert anything, and thus cannot have a problem reading any string.

Ice for C++ simply expects that the strings it receives are UTF-8 encoded, and this is what it will get from Ice for C#. If you have an UTF-8 compatible application (like an UTF-8 compatible GUI toolkit), then you can use these strings right away. Otherwise, you can also convert it to UTF-16 wstrings with the mentioned conversion functions.

If your appllication doesn't use Unicode in either UTF-8 or UTF-16 encoding, then your own application code must convert between Unicode and whatever font you are using.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
"Invalid UTF8 string" when transer chinese chars between cpp server and csharp client raygo Help Center 8 03-21-2006 09:34 PM
send an object client -> server arnaud Help Center 3 02-23-2006 08:29 PM
how can i make server send string to client jerry_cym Help Center 2 02-07-2006 05:46 AM
encoding error when Ice java client get string from Ice c++ server casper Help Center 1 09-16-2005 04:01 AM
How server initiatively send message to client, and client can responce to it? ouloba Help Center 3 11-26-2004 10:35 AM


All times are GMT -4. The time now is 06:25 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2008 ZeroC, Inc.