1、TIdUDPClient
INDY9:
TIdUDPClient1.SendBuffer('127.0.0.1',9999,Request,SizeOf(Request));
TIdUDPClient1.ReceiveBuffer(ResponseBlock,SizeOf(ResponseBlock));
INDY10:
VAR:Temp:Tbytes;
TIdUDPClient1.SendBuffer('127.0.0.1', 9999, RawToBytes(Request, SizeOf(Request)));
SetLength(Temp, SizeOf(ResponseBlock));
TIdUDPClient1.ReceiveBuffer(Temp);
bytestoraw(Temp, ResponseBlock, SizeOf(ResponseBlock));
2、TIdUDPServer
INDY9:
procedure TfrmMain.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
......
if AData.Size = SizeOf(Request) then
begin
AData.Read(Request,SizeOf(Request));
.........
ABinding.SendTo(ABinding.PeerIP,ABinding.PeerPort,FileInfo,SizeOf(FileInfo));
INDY10:
procedure TfrmMain.IdUDPServer1UDPRead(Sender: TObject; AData: TBytes;
ABinding: TIdSocketHandle);
.......
if length(AData)= SizeOf(Request) then
begin
//将接收到的Tbytes信息转化为Trequest类型的数据结构Request中
BytesToRaw(AData, Request, SizeOf(TRequest));
ABinding.SendTo(ABinding.PeerIP,ABinding.PeerPort,rawtobytes(FileInfo,SizeOf(FileInfo)));