0

Using the XMLSocket class in as3 i am trying to read the string that include special characters (including chinese symbols) sent by a .NET server that uses the Socket class in c# for communication. The log shows the data properly . But when i get it in as3, all the special characters are replaced by '?'. I thought it would be a font issue. But tried with embedding the fonts and got the same result. If i read a file having special characters using URLLoader, then everything is proper. But when i tried to read the data sent by the .net server using the flash XMLSocket class the special characters are replaced. Is there any encoding we need to set in .net Socket class or somewhere or can this be solved from the client side(flash as3) itself?

thanks in advance

singularity
  • 107
  • 1
  • 13
  • I dont use **XMLSocket** but have extractes Strings from bytes. Are you extracting XML text diretly from a byte array? If so, use `myBytes.readMultiByte( myBytes.length, "unicode");` here **unicode** is the encoding Char Set you want the text to be processed as. Check **[here for other sets](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/charset-codes.html)** (use the **label** name in your code). Also **[this tip](http://stackoverflow.com/a/1069884/2057709)** might be useful advice if you use only the XMLSocket class (no direct access to bytes?) – VC.One May 19 '16 at 09:06
  • hi thanks..so if i use **xmlsocket** i can't get special chars from it?...do you know which encoding does the xmlsocket use? – singularity May 19 '16 at 11:27
  • I don't use **XMLSocket** but checking its docs I don't think it will read the special chars since they need `readMultiByte` an option only available to bytes. Normal **Socket** class should send / receive bytes so get via that methods and extract. How are you currently reading the data (show the line of code)? When you receive any socket data do you write/copy it to a bytearray? – VC.One May 19 '16 at 11:52
  • this is the code i am using. I use the **DataEvent::data** to get the data sent from the server var mySocket:XMLSocket = new XMLSocket();` .... mySocket.addEventListener(DataEvent.DATA, dataHandler); .... private function dataHandler(e:DataEvent):void { .... myXML = new XML(e.data); // using this xml } `private function send(str:String):void {` ` mySocket.send(str);` `}` – singularity May 19 '16 at 12:30
  • I see now. `e.data` is a String. You're likely getting UTF-8 String but for Chinese characters you need UTF-16. Hence the **???** for any non UTF-8 chars. Did you try **Socket** instead of **XMLSocket**? That way you get bytes which later can be read UTF-16. – VC.One May 19 '16 at 14:30
  • But **utf8** support Chinese characters – singularity May 19 '16 at 19:38
  • And I forgot to mention that my flash player version is **10.3** – singularity May 19 '16 at 19:42
  • UTF-8 does support multilingual characters in the rest of world, but try that in Flash without extra effort... and be disappointed (especially current Pepper Flash version). Does your NET server give only XML data to every connection? Did you try getting a print of bytes (Option2 in my answer) We can't tell you how to handle what we dont know & cant see. Make a simple testing XML & update your Question saying I have these bytes (show them) – VC.One May 19 '16 at 20:18
  • tried with your Option1 `socket.readBytes(mySocketBytes)` did not read anything to mySocketBytes. I could access the socket data only as `var str:String = socket.readUTFBytes(socketevent.bytesLoaded)`. But even in that chinese characters are replaced with '?'. the xml is `` `` `` `` socket.byteAvailable was showing 0 always. – singularity May 20 '16 at 09:43
  • Thanks for showing the XML you receive. Easier to help you now. (thought your socket had incoming bytes). Hopefully the updated answer can be helpful to you in some way... – VC.One May 20 '16 at 19:39
  • PS: Dont use `readUTFBytes` on Chinese characters, instead use `readMultiByte` since UTF-8 is one byte per char and some Chinese chars use 2-bytes per char. I was trying to tell you that UTF-8 won't work when dealing with bytes in AS3 (**Socket** data is just packets of bytes). Otherwise yes UTF-8 normally can handle Chinese too. I dunno why the code didn't work since it's confirmed by AS3 Docs and extracted from a good book I got. Anyways good luck. – VC.One May 20 '16 at 22:30

2 Answers2

0

Here is some quick example code you can try with your XML texts. Since I dont have a server sending me XML nor some Socket code setup I will recreate your received data like so...

<xml>
    <root> 
    <name>chinese</name>
    <value>嗨</value> 
    </root>
</xml>;

The most important thing is to show English & Chinese texts both in one textfield using just one font (Arial). Hope it works for you & gives you a starting point. Also notice in the IDE (code & traces) the Chinese character is a square () but the Arial font can display it as true character shape ().

Test code (just paste & compile)...

// # 1) Make text box to display XML content
var txtBox : TextField = new TextField();

var txtFmt : TextFormat = new TextFormat();

txtFmt.font = "Arial"; txtFmt.color = 0x00CCFF;
//txtFmt.align = TextFormatAlign.CENTER;
txtFmt.bold = true; txtFmt.size = 35;
txtBox.selectable = true; 
txtBox.defaultTextFormat = txtFmt;

txtBox.width = 200; txtBox.height = 40;
addChild (txtBox);

// #2 Make XML (You already have this part coming from Socket & Server)
var myXML : XML = "<xml > <root> <name>chinese</name> <value>嗨</value> </root> </xml>";

//trace( "check name  : "  + ( myXML.root.name) );
//trace( "check value : "  + ( myXML.root.value) );

// # 3 Update text box on stage to see output
txtBox.text = myXML.root.name + " " + myXML.root.value;
VC.One
  • 14,790
  • 4
  • 25
  • 57
0

Finally i have found the bug. My .NET server was using Default encoding which will be different depending on the system in which the server runs. I tried with UTF8 encoding for server and it worked. If the server is sending the texts encoded in UTF8 then the XMLSocket will be able to get the Unicode characters also since it uses UTF8. adding my reference Encoding.Default Property

singularity
  • 107
  • 1
  • 13