Sysmex

I am having problems with sending Thai characters through a socket to another machine. I am trying to write a component in C#. These Thai characters appear as . There seems to be some encoding issue. Send method of Socket class accepts data as byte array only, it is during this process that my data gets corrupted. I tried Default, ASCII, UTF7, Unicode encodings without any success.

msgLength = Encoding.UTF8.GetByteCount( message );
byte[] byteMessage = new byte[msgLength];
byteMessage = Encoding.UTF8.GetBytes( message );
try
{
    // retSend is the number of bytes send to socket
    retSend = mTcpSocket.Send(byteMessage, 0, msgLength, SocketFlags.None );  
}


Thanks

MK



Re: .NET Framework Networking and Communication Problem with sending Thai Characters through sockets (Socket.Send)

Mike Flasko

Can you post your full repro code please






Re: .NET Framework Networking and Communication Problem with sending Thai Characters through sockets (Socket.Send)

Durgaprasad Gorti

First
msgLength = Encoding.UTF8.GetByteCount( message );
byte[] byteMessage = new byte[msgLength];
byteMessage = Encoding.UTF8.GetBytes( message );

Allocating bytes is unnecessary. You could just do
byte[] byteMessage = Encoding.UTF8.GetBytes( message );
Then use the byteMessage.Length

That said your other end need to know exactly what the encoding of the
bytes are, If the other end is expecting the THAI MBCS encoding, then
you have an issue. What I would suggest is do the UTF8 and then
do the reverse on the other side. That should do it





Re: .NET Framework Networking and Communication Problem with sending Thai Characters through sockets (Socket.Send)

Sysmex

I am sending the source code as follows. SendToSocket function  is part of the dll Transporter.dll. I use this dll in an asp application to send some data containing some Thai characters to another application. This dll was working fine for English characters and the receiving application was getting all the data OK and the encoding at that point was Encoding.ASCII. However for sending Thai characters I changed the encoding to UTF8.
As Durga has suggested that I should convert it into UTF8 and then do the reverse conversion at the other end. Unfortunately I cannot go for this option as I do not have any control over the receiving application. This application is happily receiving all the data if we use one of our quite old components written in C++. I hope .NET has got some solution to send the data in a format which the receiving application can handle in a correct way.


public
int SendToSocket(string message)
{

................................................
................................................
................................................

   if (mTcpSocket.Connected == true)
   {
      byte[] byteMessage = Encoding.UTF8.GetBytes( message );
      msgLength = byteMessage.Length;
      try
      {
         retSend = mTcpSocket.Send(byteMessage, 0,  msgLength, SocketFlags.None );
      }
      catch (Exception ex)
      {
         mErrorMessage = ex.Message;
         mTcpSocket.Close();
         mTcpSocket = null;
         retValue = 1;
      }
   }
return retValue;
}






Re: .NET Framework Networking and Communication Problem with sending Thai Characters through sockets (Socket.Send)

Durgaprasad Gorti

The issue is not .NET specific.

1. If the receiving application is receiving bytes and using
ASCII conversion - I mean treating each byte as a character and using the
ISO-8859-1 code page, then you have to send the bytes using ISO-8859-1 codepage.

2. What I am saying is that both the sending and receiving apps need to
mutually agree on what the bytes actually mean.
A given set of bytes can be interpreted as any code page in this list
http://msdn.microsoft.com/library/ url=/workshop/author/dhtml/reference/charsets/charset4.asp
So you can't possibly look at the bytes and tell what the code page is.

SO my recommendation to you is to look at the receiving app's spec (:-))
Or code and see what exactly it is doing after it recevies the bytes.  Until
You understand that you are basically stuck with this.







Re: .NET Framework Networking and Communication Problem with sending Thai Characters through sockets (Socket.Send)

Exclude

MK..I also have the same problem with you..Did you can solve this problem Can I get your email My email is para28@hotmail.com..krop kun ka.