Search code examples
stringdelphitcpclient-serverdelphi-xe

Delphi XE - SendText through ServerSocket returns Chinese or vietnamese characters on the other end


I wrote a simple file transfer program that runs on Windows 7. I run this program as a server on one computer and client on the other. Client sends out a request for a file transfer and then server sends out the name of the file first. Then, client acknowledges that it got the file name and to send the content of the file.

This program worked flawlessly on XP. Now we are trying to run it on Windows 7 computers and it has problem. The problem is whenever the server replies back with the filename to the client.

Server sends text by calling ServerSocket1.SendText('File1.dat').

What the client gets looks like either Chinese or Vietnamese characters. So, my program fails. My client program has to know the name of the file. So, it knows where to save it in specific location in hardrive.

I think, SendText function takes AnsiString and What I am sending is string data. Do you think that's the reason?

UPDATE

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
   Socket.SendText(AnsiString('calibrate.log'));
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
   Buffer:array[0..999] of char;
begin
   Socket.ReceiveBuf(Buffer,Socket.ReceiveLength);
end;

Solution

  • Well, your problems come from the fact that you send your data as AnsiString, and read it with WideChars (Char is an alias of WideChar in Delphi XE).

    Changing your code for this would most likely fix your problem.

    procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    var    
      Buffer:array[0..999] of Ansichar; 
    begin    
      Socket.ReceiveBuf(Buffer,Socket.ReceiveLength); 
    end;