Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Created September 6, 2010 03:08
Show Gist options
  • Save huacnlee/566590 to your computer and use it in GitHub Desktop.
Save huacnlee/566590 to your computer and use it in GitHub Desktop.
procedure TForm1.UDPUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
iType: Integer;
pBuf: Pointer;
str: String;
buf: array[0..8191] of char;
begin
pBuf := AllocMem(AData.Size);
try
AData.ReadBuffer(pBuf^, AData.Size);
iType := 0;
CopyMemory(@iType, pBuf, 4);
if iType <> 1001 then Exit;
SetLength(str, AData.Size-4);
CopyMemory(@str[1], Pointer(DWORD(pBuf)+4), AData.Size-4);
Memo1.Lines.Add(ABinding.PeerIP+' '+ IntToStr(ABinding.PeerPort)+':'+str);
UDP.Send(ABinding.PeerIP, ABinding.PeerPort, '接收消息成功!');
finally
FreeMem(pBuf);
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment