Archived Forum Post

Index of archived forum posts

Question:

Socket.SendBytes Sends Twice as Many Bytes?

Jun 01 '16 at 08:23

Question: One of my application's tasks is to send a file from FoxPro to a server running PHP. Until now communication worked like a charm - I was using the ReceiveUntilMatch method.

Now, after executing FILETOSTR in VFP, I receive a string (length for example 129599) and then send it with the SendBytes method. And here is a problem - I think Chilkat is encoding passed string to the 2 byte representation, because in PHP I receive exactly double the length (in this case 259198).

For the test I have sent same file from standard socket to the Chilkat socket in PHP and there was no problem. (The PHP side socket is receiving data by ReceiveBytes method.)


Answer

In any programming language that uses the ActiveX, such as FoxPro, PowerBuilder, VB6, Delphi, Classic ASP, etc., if you have a string variable, it is always passed as utf-16 (2 bytes per char). (ActiveX uses COM interfaces, and COM interfaces use BSTR's for passing strings.)

If you pass a string to a method that requires bytes, you are implicitly passing the utf-16 byte representation of the characters to the method. (2 bytes per char). You should instead send the string by calling the socket.SendString method. The socket.StringCharset property controls the byte representation of the characters being sent. The default value of StringCharset is "ansi", which sends characters using the ANSI character encoding (1 byte per char for Western European languages). You could set StringCharset = "utf-8" if you wanted to send the utf-8 byte representation of the string. If you set the StringCharset property equal to "utf-16", then you'd duplicate the implicit behavior you see w/ SendBytes.

If you are unfamiliar with character encoding (i.e. the byte representation of characters), then see this: https://www.example-code.com/charset101.asp