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(开发者_StackOverflow'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;
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;
TClientSocket is deprecated since Delphi 6 (see Is Delphi TClientSocket (still) deprecated?) so I would expect problems with Unicode data and in other areas. As written in one of the answers, TClientSocket and TServerSocket also use ineffective design based on Windows messages. So I would try to use Indy or Synapse instead. This also would make it ready for cross-platform usage (Windows messages are obviuousle not available on OSX).
I resolved my question. I am not sure why it works flawlessly on Windows XP.
Anyways, I am sending and receiving texts as follows. I originally was reading the text into array of chars through ReceiveBuf method.
Socket.SendText('File.log');
theStr:String;
theStr := Socket.ReceiveText;
Thanks for helping me realize my own programming issue.
精彩评论