It most likely is. Better close the connection first (after checking if it's done of course) and only then free the memory.
Not sure, but you may have to close the stream as well (don't have Delphi on this computer).
Main Topics
Browse All TopicsHi,
I am trying to make a image transmission program and a image receiving program on two different computers. The idea is that I load an JPEG image in the transmission program and when I push a send button it should send the JPEG image as a stream to the other computer. I use the NMUDP component on both computers. But I keep getting an error 'access violation at 0x77f60b6f: write of address 0xfd4' when I push the send button... Can anyone see what I am doing wrong... Or does anyone have some source code, that sends and receives an image using UDP.... My procedure is shown below:
procedure TTransmitterForm.btnTransm
var
MyStream: TMemoryStream;
Jpg: TJPEGImage;
begin
NMUDP.RemoteHost := Edit1.Text;
NMUDP.RemotePort := StrToInt(Edit2.Text);
//Create a JPEG image and assign the loaded picture
Jpg := TJPEGImage.Create;
Jpg.Assign(Image.Picture);
try
// Create a stream and save the image to the stream
MyStream := TMemoryStream.Create;
Jpg.SaveToStream(MyStream)
MyStream.Position := 0;
NMUDP.SendStream(MyStream)
finally
MyStream.Free;
Jpg.Free;
end;
end;
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
from the TNMUDP help:
create the Stream before the try/finally...
if you have an OnInvalidHost:
When the OnInvalidHost event is called, the InputQuery function (function in delphi OnInvalidHost example) is called, allowing the user to input a new host name to replace the invalid one previously entered. If the user clicks the Ok Button, the handled parameter is set to TRUE, and the component attempts it's action again with the new hostname. If the hostname is still invalid, or the cancel button was clicked in the dialog, an exception is raised.
the Delphi Help example:
procedure TForm1.Button1Click(Sender
var
MyStream: TMemoryStream;
C: String;
begin
C := Edit1.Text;
NMUDP1.RemoteHost := '127.0.0.1';
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.RemotePort := 6668;
MyStream := TMemoryStream.Create;
try
MyStream.Write(C[1], Length(C));
NMUDP1.SendStream(MyStream
finally
MyStream.Free;
end;
end;
When Button1 is clicked, the text in Edit1 is written into the stream MyStream. The SendStream method is used to send the stream to the remote host, which is 127.0.0.1 (local host) in this instance. Note that MyStream is utilized within a try...finally loop to prevent a memory leak.
of course, that's an example about sending strings...
my post was about the freeing on finally, and that's ok in your source too...the difference is in the stream creation (before the try...finally) and if you're passing some code to the OnInvalidHost event setting Handle to true, but the Host is not valid, this raises an exception...
Hi,
The code below works fine with very small pictures but not with bigger ones:
There are one NMUDP, one Button and two Image components on a form. Image1 has a small bmp loaded in design-time and Image2 is empty. When you press the button, the picture from Image1 is being transferred via NMUDP and assigned to Image2.
------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NMUDP, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
NMUDP1: TNMUDP;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure NMUDP1DataReceived(Sender:
FromIP: String; Port: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses jpeg;
procedure TForm1.FormCreate(Sender: TObject);
begin
NMUDP1.RemoteHost := '127.0.0.1';
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.LocalPort := 6668;
NMUDP1.RemotePort := 6668;
end;
procedure TForm1.Button1Click(Sender
var
MyStream: TMemoryStream;
Jpg: TJPEGImage;
begin
//Create a JPEG image and assign the loaded picture
Jpg := TJPEGImage.Create;
Jpg.Assign(Image1.Picture.
try
// Create a stream and save the image to the stream
MyStream := TMemoryStream.Create;
try
Jpg.SaveToStream(MyStream)
NMUDP1.SendStream(MyStream
finally
MyStream.Free;
end;
finally
Jpg.Free;
end;
end;
procedure TForm1.NMUDP1DataReceived(
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
Jpg: TJPEGImage;
begin
try
Jpg := TJPEGImage.Create;
try
MyStream := TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream
MyStream.Position := 0;
Jpg.LoadFromStream(MyStrea
Image2.Picture.Assign(jpg)
finally
MyStream.Free;
end;
finally
Jpg.Free;
end;
except
end;
end;
end.
-------
Regards, Geo
OnDataReceived event should be:
procedure TForm1.NMUDP1DataReceived(
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
Jpg: TJPEGImage;
begin
Jpg := TJPEGImage.Create;
try
MyStream := TMemoryStream.Create;
try
NMUDP1.ReadStream(MyStream
Jpg.LoadFromStream(MyStrea
Image2.Picture.Assign(jpg)
MyStream.Clear;
finally
MyStream.Free;
end;
finally
Jpg.Free;
end;
end;
Regards, Geo
Take a look at http://community.borland.c
The article come with a sample project.
Tango42:
If you are using Indy, then you do not have to worry about the packet size; because it takes care of that for you
Regarding your original error; most likely; you are getting the error because you are not converting the jpg correctly.
here is your original code after adding three more lines to it.
procedure TTransmitterForm.btnTransm
var
MyStream: TMemoryStream;
Jpg: TJPEGImage;
begin
NMUDP.RemoteHost := Edit1.Text;
NMUDP.RemotePort := StrToInt(Edit2.Text);
//Create a JPEG image and assign the loaded picture
Jpg := TJPEGImage.Create;
Jpg.Assign(Image.Picture);
try
JPG.CompressionQuality := 50; // or whatever you need
JPG.JPEGNeeded;
JPG.Compress;
// Create a stream and save the image to the stream
MyStream := TMemoryStream.Create;
Jpg.SaveToStream(MyStream)
MyStream.Position := 0;
NMUDP.SendStream(MyStream)
finally
MyStream.Free;
Jpg.Free;
end;
end;
Business Accounts
Answer for Membership
by: ziolkoPosted on 2003-06-17 at 01:34:24ID: 8738617
>>MyStream.Free I'm not sure but calling free before stream is sent may be a problem.
ziolko.