Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1208
  • Last Modified:

TMemoryStream + TServerSocket

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
Karamja32
Asked:
Karamja32
  • 6
  • 3
1 Solution
 
shaneholmesCommented:

 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
 
shaneholmesCommented:
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
 
Karamja32Author Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
shaneholmesCommented:
OK let me make a project out of it

Shane
0
 
Karamja32Author Commented:
Thx, Send to root@k-32.co.uk or upload to a website if you have one.
0
 
shaneholmesCommented:
I will mail it to:  root@k-32.co.uk

Sahne
0
 
shaneholmesCommented:
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
 
Karamja32Author Commented:
Ok, Thanks and I do have two computers :D
0
 
shaneholmesCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now