Solved

TMemoryStream + TServerSocket

Posted on 2004-04-24
9
1,185 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
Industry Leaders: 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 160 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month3 days, 11 hours left to enroll

630 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