pete_03
asked on
send receive TMemorystream over UDP
hi all,
i trying like to porting my software that was compiled by delphi 6 to delphi 2007;
unfortunately, i can't found the subtitute for nmUDP (netmaster) component which have methode :
readstream, and sendstream of Tmemorystream.
would you like to write some example code(methode) for sender and listener:
1. using UDP
2. send a TMEmorystream
3. the listener (aSocket or whatever) can receive data as a Tmemorystream or processed to be TMemory stream.
4. whatever the component(indy, ics, synapse, etc...) used as long it's work on delphi 2007, it's ok
thanks
i trying like to porting my software that was compiled by delphi 6 to delphi 2007;
unfortunately, i can't found the subtitute for nmUDP (netmaster) component which have methode :
readstream, and sendstream of Tmemorystream.
would you like to write some example code(methode) for sender and listener:
1. using UDP
2. send a TMEmorystream
3. the listener (aSocket or whatever) can receive data as a Tmemorystream or processed to be TMemory stream.
4. whatever the component(indy, ics, synapse, etc...) used as long it's work on delphi 2007, it's ok
thanks
ASKER
thanks for reply, i am beginner of delphi, could wrap that code to a methode like:
procedure sendstream(aMemoryStream: TMemorystream) as easy as : nmUDP1.sendstream(myMemory stream)
and how about the listener ?
procedure sendstream(aMemoryStream: TMemorystream) as easy as : nmUDP1.sendstream(myMemory
and how about the listener ?
show me your exact example of what you ant (i.e. your nmUDP code)
and I will show you how to do the equivalent in Indy.
The bit I need to understand is if you need to send the "size" of the stream first, or if you do not need the size
and I will show you how to do the equivalent in Indy.
The bit I need to understand is if you need to send the "size" of the stream first, or if you do not need the size
ASKER
procedure TMainModule.SendRecordValu e(FRemoteH ost: String; FPort: Integer);
var FMemoryStream: TMemoryStream;
i : Byte;
FValue : Single;
begin
FMemoryStream := TMemoryStream.Create;
try;
for i := 0 to (record.Count-1) do
begin
FValue := record.Value[i];
FMemoryStream.Write(FValue , SizeOf(FValue));
end;
NMUDP1.RemoteHost := FRemoteHost;
NMUDP1.RemotePort := FPort;
NMUDP1.SendStream(FMemoryS tream);
finally
FMemoryStream.Free;
end;
end;
procedure TfrmMonitoring.NMUDP1DataR eceived(Se nder: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
FMemoryStream : TMemoryStream;
FValue : Single;
I : Integer;
begin
if NumberBytes > 0 then
begin
FByteReceived := FByteReceived + NumberBytes;
FMemoryStream := TMemoryStream.Create;
try
NMUDP1.ReadStream(FMemoryS tream);
FMemoryStream.Position := 0;
i := 0;
repeat
FMemoryStream.Read(FValue, SizeOf(FValue));
ClientRecord.Value[i] := FValue;
Inc(i);
until (FMemoryStream.Position >= FMemoryStream.Size);
finally
FMemoryStream.Free;
end;
end;
var FMemoryStream: TMemoryStream;
i : Byte;
FValue : Single;
begin
FMemoryStream := TMemoryStream.Create;
try;
for i := 0 to (record.Count-1) do
begin
FValue := record.Value[i];
FMemoryStream.Write(FValue
end;
NMUDP1.RemoteHost := FRemoteHost;
NMUDP1.RemotePort := FPort;
NMUDP1.SendStream(FMemoryS
finally
FMemoryStream.Free;
end;
end;
procedure TfrmMonitoring.NMUDP1DataR
NumberBytes: Integer; FromIP: String; Port: Integer);
var
FMemoryStream : TMemoryStream;
FValue : Single;
I : Integer;
begin
if NumberBytes > 0 then
begin
FByteReceived := FByteReceived + NumberBytes;
FMemoryStream := TMemoryStream.Create;
try
NMUDP1.ReadStream(FMemoryS
FMemoryStream.Position := 0;
i := 0;
repeat
FMemoryStream.Read(FValue,
ClientRecord.Value[i] := FValue;
Inc(i);
until (FMemoryStream.Position >= FMemoryStream.Size);
finally
FMemoryStream.Free;
end;
end;
I mostly use a TStringList to send strings ...
you may need to check if ClientRecord.RecordCount works
you may need to check if ClientRecord.RecordCount works
procedure TForm1.SendRecordValue(aRemoteHost: String; aPort: Integer);
var i : Byte;
List: TStringList;
begin
List := TStringList.Create;
try
for i := 0 to (record.Count-1) do
List.Add(Record.Value[i]);
IdUDPClient1.Send(aRemoteHost, aPort, List.Text);
finally
List.Free;
end;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
I : Integer;
List: TStringList;
Stream: TStringStream;
begin
ClientRecord.RecordCount := 0;
if AData.Size > 0 then
begin
List := TStringList.Create;
try
Stream := TStringStream.Create('');
try
Stream.CopyFrom(AData, AData.Size);
List.Text := Stream.DataString;
finally
Stream.Free;
end;
ClientRecord.RecordCount := List.Count;
for I := 0 to List.Count - 1 do
ClientRecord.Value[i] := List[I];
finally
List.Free;
end;
end;
end;
ASKER
on idUDP server, i doubleclick at onUDPRead Event then on editor:
procedure TForm1.IdUDPServer1UDPRead (Sender: TObject; AData: TBytes;
ABinding: TIdSocketHandle);
procedure TForm1.IdUDPServer1UDPRead (Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
it's raises error, the parameters AData should be Tbytes instead TStream.
then i forced to change the parameters type to TStream as you suggest, application compiled successfully, i set a breakpoint at this event, nothing fired when the client has sent a udp packet?
i am using indy10, D2007 default packages
procedure TForm1.IdUDPServer1UDPRead
ABinding: TIdSocketHandle);
procedure TForm1.IdUDPServer1UDPRead
ABinding: TIdSocketHandle);
it's raises error, the parameters AData should be Tbytes instead TStream.
then i forced to change the parameters type to TStream as you suggest, application compiled successfully, i set a breakpoint at this event, nothing fired when the client has sent a udp packet?
i am using indy10, D2007 default packages
oops, that was for Delphi 7
this is for Delphi 2007
this is for Delphi 2007
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TBytes; ABinding: TIdSocketHandle);
var
I : Integer;
List: TStringList;
Stream: TStringStream;
begin
//ClientRecord.RecordCount := 0;
if Length(AData) > 0 then
begin
List := TStringList.Create;
try
Stream := TStringStream.Create('');
try
Stream.Read(AData, Length(AData));
List.Text := Stream.DataString;
ClientRecord.RecordCount := List.Count;
for I := 0 to List.Count - 1 do
ClientRecord.Value[i] := List[I];
finally
Stream.Free;
end;
finally
List.Free;
end;
end;
end;
procedure TForm1.SendRecordValue(aRemoteHost: String; aPort: Integer);
var i : Byte;
List: TStringList;
begin
List := TStringList.Create;
try
for i := 0 to (record.Count-1) do
List.Add(Record.Value[i]);
IdUDPClient1.Host := aRemoteHost;
IdUDPClient1.Port := aPort;
IdUDPClient1.Send(List.Text);
finally
List.Free;
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
here's a very simple example I wrote using a TStringStream...
using a TMemoryStream is similar, but a stringstream is easier to demonstrate
Open in new window