0

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;
ThN
  • 3,235
  • 3
  • 57
  • 115
  • Show us the code. Are you running the same exe on both XP and Win7? And yes, sending ANSI encoded text to a pipe expecting UTF-16 encoded text results in Chinese text being received! – David Heffernan Sep 28 '11 at 16:05
  • @ David, Yes I run the same exe on Windows 7 systems only. – ThN Sep 28 '11 at 16:06
  • @DavidHeffernan, I see. When compile it gives me warnings for sendtext method that it requires AnsiString and that through implicit conversion I may loose data. So, I typecast it explicitly to AnsiString as shown above. – ThN Sep 28 '11 at 16:14
  • You are sending ANSI encoded text, but your receiver is interpreting that as UTF-16. What is your receiver doing? – David Heffernan Sep 28 '11 at 16:21
  • It gets something from the socket and displays Chinese characters. – ThN Sep 28 '11 at 16:28
  • clearly it interprets the stream as UTF16 – David Heffernan Sep 28 '11 at 17:18
  • 1
    @digitalanalog: please show your client code that is receiving the filename. Note that `TClientSocket` and `TServerSocket` DO NOT implement support for `UnicodeString` correctly, so you are better off encoding the string data, say to UTF-8, and then use `SendBuf()` and `ReceiveBuf()` instead. – Remy Lebeau Sep 28 '11 at 18:25
  • @RemyLebeau-Team, it was just that I was using SendText and ReceiveBuf that probably got all confused. so, I sendtext and receiveText. It works great. – ThN Sep 28 '11 at 19:10
  • 2
    @digitalanalog: `SendText()` and `ReceiveText()` operate on `AnsiString`, not on `UnicodeString` or even `RawByteString`. `AnsiString` is codepage-aware in D2009+, which means an implicit data conversion is going to occur when calling `SendText()` and `ReceiveText()` may return a bad `AnsiString` whose data does not match its codepage correctly. – Remy Lebeau Sep 28 '11 at 21:09

3 Answers3

2

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; 
Ken Bourassa
  • 6,363
  • 1
  • 19
  • 28
1

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).

Community
  • 1
  • 1
mjn
  • 36,362
  • 28
  • 176
  • 378
  • @ mjn Interesting. I will have to implement your suggestion in my next revision. Thank you. – ThN Sep 29 '11 at 13:08
0

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.

ThN
  • 3,235
  • 3
  • 57
  • 115