Delphi XE - 通过ServerSocket发送文本在另一端返回中文或越南语字符

发布于 2024-12-07 04:16:21 字数 781 浏览 3 评论 0原文

我编写了一个在 Windows 7 上运行的简单文件传输程序。我在一台计算机上将该程序作为服务器运行,在另一台计算机上作为客户端运行。客户端发出文件传输请求,然后服务器首先发出文件名。然后,客户端确认已获取文件名并发送文件内容。

该程序在 XP 上完美运行。现在我们尝试在 Windows 7 计算机上运行它,但出现问题。问题是每当服务器将文件名回复给客户端时。

服务器通过调用 ServerSocket1.SendText('File1.dat') 发送文本。

客户端得到的看起来要么是中文字符,要么是越南字符。所以,我的程序失败了。我的客户端程序必须知道文件的名称。因此,它知道将其保存在硬盘驱动器中的特定位置。

我认为,SendText 函数采用 AnsiString,我发送的是字符串数据。你认为是这个原因吗?

更新

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风透绣罗衣 2024-12-14 04:16:21

好吧,您的问题来自于您将数据作为 AnsiString 发送,并使用 WideChars 读取它(Char 是 Delphi XE 中 WideChar 的别名)。

为此更改您的代码很可能会解决您的问题。

procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
var    
  Buffer:array[0..999] of Ansichar; 
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; 
硪扪都還晓 2024-12-14 04:16:21

TClientSocket 自 Delphi 6 起已被弃用(请参阅
Delphi TClientSocket(仍然)已弃用吗?)所以我预计 Unicode 数据会出现问题在其他领域。正如答案之一所写,TClientSocket和TServerSocket也使用了基于Windows消息的无效设计。所以我会尝试使用 Indy 或 Synapse 来代替。这也使其可以跨平台使用(Windows 消息显然在 OSX 上不可用)。

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

无力看清 2024-12-14 04:16:21

我解决了我的问题。我不知道为什么它在 Windows XP 上运行得很好。

无论如何,我正在发送和接收短信如下。我最初是通过 ReceiveBuf 方法将文本读入字符数组。

Socket.SendText('File.log');

theStr:String;
theStr := Socket.ReceiveText;

感谢您帮助我认识到我自己的编程问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文