Solved

TMemoryStream + TServerSocket

Posted on 2004-04-24
9
1,168 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

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