Solved

TMemoryStream + TServerSocket

Posted on 2004-04-24
9
1,159 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
  • 6
  • 3
9 Comments
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility

 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
Comment Utility
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
Comment Utility
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
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
OK let me make a project out of it

Shane
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

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

Expert Comment

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

Sahne
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
Comment Utility
Ok, Thanks and I do have two computers :D
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 160 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now