Home Help Center

Why the Chinese character pass between application write with C++ and C# has question

lotusqinlotusqin Member
When I pass Chinese character as parameter from ICE client application write with C# to ICE server application ,I find the ICE server application recieved the parameter not crrect (Client send 2 characters but Server recieved 3 characters and not inclue the 2 characters witch the client send,they are all different.)
I write the "Hello.ice" file ,changed the "nonmutating void sayHello();" to "nonmutating void sayHello(string ps);" ,the "ps" is the parm I pass Chinese character .
I rework the "hello world" demo server project write with C++ ,I change the function "sayHello" of classs HelloI as next
HelloI::sayHello(const string & ps,const Ice::Current&) const
{
//cout << "Hello World!" << endl;
cout << ps << endl;
}
Then I rework the "hello world" demo client project write with C# .I change the function "run" of class client on the line followed "if(line.Equals("t"))" ,you can see the code with notes "//two Chinese characters " :
private static int run(string[] args, Ice.Communicator communicator)
{
Ice.Properties properties = communicator.getProperties();
string proxyProperty = "Hello.Proxy";
string proxy = properties.getProperty(proxyProperty);
if(proxy.Length == 0)
{
Console.Error.WriteLine("property `" + proxyProperty + "' not set");
return 1;
}

Ice.ObjectPrx basePrx = communicator.stringToProxy(proxy);
HelloPrx twoway = HelloPrxHelper.checkedCast(basePrx.ice_twoway().ice_timeout(-1).ice_secure(false));
if(twoway == null)
{
Console.Error.WriteLine("invalid proxy");
return 1;
}
HelloPrx oneway = HelloPrxHelper.uncheckedCast(twoway.ice_oneway());
HelloPrx batchOneway = HelloPrxHelper.uncheckedCast(twoway.ice_batchOneway());
HelloPrx datagram = HelloPrxHelper.uncheckedCast(twoway.ice_datagram());
HelloPrx batchDatagram = HelloPrxHelper.uncheckedCast(twoway.ice_batchDatagram());

int timeout = -1;

menu();

string line = null;
string ss;
do
{
try
{
Console.Out.Write("==> ");
Console.Out.Flush();
line = Console.In.ReadLine();
if(line == null)
{
break;
}
if(line.Equals("t"))
{
ss="好好";// two Chinese characters
twoway.sayHello(ss);
}
else if(line.Equals("o"))
{
oneway.sayHello("阿");
}
else if(line.Equals("O"))
{
batchOneway.sayHello("阿");
}
else if(line.Equals("d"))
{
datagram.sayHello("阿");
}
else if(line.Equals("D"))
{
batchDatagram.sayHello("阿");
}
else if(line.Equals("f"))
{
communicator.flushBatchRequests();
}
else if(line.Equals("T"))
{
if(timeout == -1)
{
timeout = 2000;
}
else
{
timeout = -1;
}

twoway = HelloPrxHelper.uncheckedCast(twoway.ice_timeout(timeout));
oneway = HelloPrxHelper.uncheckedCast(oneway.ice_timeout(timeout));
batchOneway = HelloPrxHelper.uncheckedCast(batchOneway.ice_timeout(timeout));

if(timeout == -1)
{
Console.WriteLine("timeout is now switched off");
}
else
{
Console.WriteLine("timeout is now set to 2000ms");
}
}
else if(line.Equals("s"))
{
twoway.shutdown();
}
else if(line.Equals("x"))
{
// Nothing to do
}
else if(line.Equals("?"))
{
menu();
}
else
{
Console.WriteLine("unknown command `" + line + "'");
menu();
}
}
catch(System.Exception ex)
{
Console.Error.WriteLine(ex);
}
}
while (!line.Equals("x"));

return 0;
}
After I run the two application,they are show as the attach Files I send.

Comments

  • benoitbenoit Rennes, FranceAdministrators, ZeroC Staff Benoit FoucherOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Your program works fine for me with the following code for the C++ server:
    #include <IceUtil/Unicode.h>
    
    void
    HelloI::sayHello(const std::string& s, const Ice::Current&) const
    {
        wcout << IceUtil::stringToWstring(s) << endl;
    }
    

    In C++, you need to use the IceUtil::stringToWstring() and IceUtil::wstringToString() to convert from string to wstring and vice versa.

    Hope this helps!

    Benoit.
  • lotusqinlotusqin Member
    Thank you very much for your answer! I take it, Unicode is the matter.
Sign In or Register to comment.