Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TMemoryStream + TServerSocket

Posted on 2004-04-24
9
Medium Priority
?
1,195 Views
Last Modified: 2010-04-05
Hello,

I need a Server and a Client.

Client sends a command, Client.Socket.SendText('Boo');
When server reads this server takes a desktop screenshot and saves into the stream, then sends
Then Client gets the stream and shows it in ImageBox, Image1.Picture.Bitmap.LoadFromStream(Stream); ??????

Thanks Karamja
0
Comment
Question by:Karamja32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10908796

 sStream := TMemoryStream.Create;
 WriteStreamBitmap(sStream, MyBMP);
 ServerSocket1.Socket.Connections[0].SendStream(sStream);


Shane


procedure WriteStreamBitmap(Stream: TStream; Bitmap: TBitmap);
var  temp:       TMemoryStream;
     size:       Integer;
begin

  // Create temp memory stream
  temp:=TMemoryStream.Create;

  // Save bitmap to temp stream
  Bitmap.SaveToStream(temp);

  // Get size of bitmap stream
  size:=temp.Size;

  // Write the size to user stream
  Stream.Write(size, SizeOf(Integer));

  // Copy the bitmap data to the user stream
  Stream.CopyFrom(temp, 0);

  // Free temp stream
  temp.Free;

end;


function ReadStreamBitmap(Stream: TStream): TBitmap;
var  temp:       TMemoryStream;
     size:       Integer;
begin

  // Create bitmap result
  result:=TBitmap.Create;

  // Create temp memory stream
  temp:=TMemoryStream.Create;

  // Read the size integer from the user stream
  Stream.Read(size, SizeOf(Integer));

  // Check for zero, because we DON'T want to read in the
  // whole user stream
  if (size > 0) then temp.CopyFrom(Stream, size);
  temp.Position:=0;

  // Now load the bitmap from the temp stream
  result.LoadFromStream(temp);

  // Free temp stream
  temp.Free;

end;




0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10908847
OTTOMH

var
  count :Integer;
  B : Array[0..1023] of byte;  
  sStream: TMemorySTream;
begin
 sStream:= TMemorySTream.Create;
  repeat
    Socket.Lock;
    count:= Socket.ReceiveBuf(B,SizeOf(B));
    if count > 0 then
      sStream.WriteBuffer(B, count);
    Socket.Unlock;
   until (count <= 0);
 Image1,Picture.Bitmap.Assign(ReadStreamBitmap(sStream));
 sStream.Free;
end;
0
 

Author Comment

by:Karamja32
ID: 10908872
Thx for your reply, But I cant get the code to work, Can you make project with the ServerSocket and ClientSocket, and email to root@k-32.co.uk, Or upload to a webserver if you have a site.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10908888
OK let me make a project out of it

Shane
0
 

Author Comment

by:Karamja32
ID: 10908892
Thx, Send to root@k-32.co.uk or upload to a website if you have one.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10908899
I will mail it to:  root@k-32.co.uk

Sahne
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10909086
OK, i sent it.

This is a skeleton i was working on for a game. I eliminated some stuff from it.

It does not use the TServerSocket & TClientSocket visual components. I create them at Runtime.
And then I assign all the events at runtime.

You will see from the code.


Anyway, start up to instances (do you have two computers)

Click create button on the one which will be the server

Click join button on the one which will be client


I use 192.168.2.1 (my address of machine running server)
and port 1024


Then click the load button to load a image
and click send to send the image!


Shane
0
 

Author Comment

by:Karamja32
ID: 10909097
Ok, Thanks and I do have two computers :D
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 640 total points
ID: 10909111
The probelm was in th Stream.Position, which i wasn't setting to Zero in the code above, but i fixed it in the code i sent you.

Shane

procedure TfrmMain.ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  count :Integer;
  B : Array[0..1023] of byte;
  sStream: TMemorySTream;
begin
 ShowMessage('Reading');
 sStream:= TMemorySTream.Create;
 sSTream.Position:= 0;
  repeat
    Socket.Lock;
    count:= Socket.ReceiveBuf(B,SizeOf(B));
    if count > 0 then
      sStream.WriteBuffer(B, count);
    Socket.Unlock;
   until (count <= 0);
  sStream.Position:=0;
  Image2.Picture.Bitmap.Assign(ReadStreamBitmap(sStream));
 sStream.Free;
end;


procedure TfrmMain.btnSendClick(Sender: TObject);
var
 sStream: TMemoryStream;
begin
 sStream := TMemoryStream.Create;
  sStream.Position:=0;
 WriteStreamBitmap(sStream, Image1.Picture.Bitmap);
 sStream.Position:=0;
 ServerSocket.Socket.Connections[0].SendStream(sStream);
end;
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question