簡體   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