gpt4 book ai didi

delphi - 在Delphi 2010中使用套接字发送和接收文本

转载 作者:行者123 更新时间:2023-12-03 19:31:46 25 4
gpt4 key购买 nike

我对Delphi 2010不太熟悉,在使用组件ClientSocket和ServerSocket时遇到了麻烦。问题很简单:我正在尝试使用以下代码将文本从客户端发送到服务器:

cliente.Socket.SendText('call');


在服务器端,我编写了以下代码:

procedure TForm6.ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
s: string;
begin
s:=Socket.ReceiveText;
if s = 'call' then
begin
showmessage('Client is Calling');
end;
end;


但是,服务器不显示该消息。能再帮我一次吗?

最佳答案

在D2009 +中,SendText()ReceiveText()不能与Unicode字符串一起正常使用。最好直接使用SendBuf()ReceiveBuf()

话虽如此,TClientSocketTServerSocket已被弃用很长时间了。您应该使用其他组件集,例如Indy(Delphi附带),例如:

IdTCPClient1.IOHandler.WriteLn('call');




procedure TForm6.IdTCPServer1Execute(AContext: TIdContext);
var
s: string;
begin
s := AContext.Connection.IOHandler.ReadLn;
if s = 'call' then
begin
// TIdTCPServer is a multi-threaded component,
// but ShowMessage() is not thread-safe...
Windows.MessageBox(0, 'Client is Calling', '', MB_OK);
end;
end;

关于delphi - 在Delphi 2010中使用套接字发送和接收文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17016086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com