Delphi 7 中使用IdUDPServer1和IdUDPClient1控件实现通信检测

Delphi 7 中使用IdUDPServer1和IdUDPClient1控件实现通信,可是如何检测两台电脑已经连通了?代码怎么写啊?求大神

D7确实经典,但已经落伍了,用xe版本吧

udp发送这个东西不检测是否通不通,用TCP连接可以检测;
实在想用可以在server发送一个字符串,client接受到以后,反馈一个什么的字符串,表示正常通讯了

在Client端:
procedure TUDPMainForm.SendButtonClick(Sender: TObject);
var
MessageID: Integer;
ThisMessage: String;
ReceivedString: String;
begin
MessageID := Random(MAXINT);
ThisMessage := 'Message: ' + IntToStr(MessageID);
UDPMemo.Lines.Add('Sending ' + ThisMessage);
UDPClient.Send(ThisMessage);
ReceivedString := UDPClient.ReceiveString(); //该方法取得由服务端返回的信息。
if ReceivedString = '' then
UDPMemo.Lines.Add('No response received from the server after ' + IntToStr(UDPClient.ReceiveTimeout) + ' millseconds.')
else
UDPMemo.Lines.Add('Received: ' + ReceivedString)
end;

Server端:

procedure TUDPMainForm.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
s: String;
begin
DataStringStream := TStringStream.Create('');
try
DataStringStream.CopyFrom(AData, AData.Size);
UDPMemo.Lines.Add('Received "' + DataStringStream.DataString + '" from ' + ABinding.PeerIP + ' on port ' + IntToStr(ABinding.PeerPort));
s := 'Replied from ' + UDPServer.LocalName + ' to "' + DataStringStream.DataString + '"';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, s[1], Length(s));
finally
DataStringStream.Free;
end;

转载于:https://www.cnblogs.com/jiangxm/p/6307002.html

你可能感兴趣的:(网络)