I'm having problems trying to use OnUDPRead Event of TIdUDPServer to read broadcast Data sent from a IdUDPClient Client I created. I've tried using the examples shown in the following questions but to no avail.
How can I send a broadcast message in Delphi
Reading data with TIdUDPServer
I'm able to bind TIdUDPServer to the port I specify:
procedure TForm1.Button1Click(Sender: TObject);
begin
IdUDPServer1.BroadcastEnabled := True;
IdUDPServer1.DefaultPort := StrToInt(edit2.Text);
IdUDPServer1.Bindings.Add.IP := '0.0.0.0';
//IdUDPServer1.ThreadedEvent:=True;
IdUDPServer1.Active := True;
end;
IdUDPServer1UDPRead is triggered successfully showing that the UDP Server is working, but I get an exception at this line -> DataStringStream.CopyFrom(AData, AData.Size);
Exception:Access violation at address 004BA415 in module 'IndyUDPReceiver.exe'. Read of address 74736574
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject;
AData: TStream; ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
msg: string;
begin
try
DataStringStream := TStringStream.Create('');
try
DataStringStream.CopyFrom(AData, AData.Size);
msg := DataStringStream.DataString;
Memo1.Lines.Add(msg);
finally
DataStringStream.Free;
end;
except
on E: Exception do
begin
Memo1.Lines.Add('Exception:' + E.Message);
DataStringStream.Free;
end;
end;
end;
I've uploaded the full Client and Server to: http://www.2shared.com/file/5SRweGIa/Indy_UDP.html
Grateful for any pointers. :)