繁体   English   中英

使用Delphi 2010中的套接字发送和接收文本

[英]Send and Receive text using sockets in Delphi 2010

我对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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM