Solved

Sending formatted text over a socket connection

Posted on 2001-06-04
4
420 Views
Last Modified: 2010-04-06
     I am developing a Chat application using the delphi socket components. What should be the type of sockets in both the Client and socket components?.

      How will I send the contents of a Rich Edit control having formatted text over the Socket connection so that it can be accessed at the other end of the connection in the same format?.

    How will I place the Chat form(each user will have a separate form)Icon in the  Task Bar or in the system tray?.

Please help me?...

0
Comment
Question by:mrk_raj
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Cesario
ID: 6153185
Hir mrk_raj,

to send the contents of a richt edit Control:

procedure TForm1.Button1Click(Sender: TObject);
var
  MemoryStream:TMemoryStream;
begin
  MemoryStream:=TMemoryStream.Create;
  RichEdit1.Lines.SaveToStream(MemoryStream);
  MemoryStream.Seek(0,soFromBeginning);
  ClientSocket.Socket.SendStream ( MemoryStream);
end;


To read the contents form a Stream:
RichEdit2.Lines.LoadFromStream(MemoryStream);


Best Regards

Cesario
0
 

Author Comment

by:mrk_raj
ID: 6155066
hai Cesario,

  For sending the contents of a richedit control through
socket connection we can use the method sendstream.But
I couldn't find any method for receiving the stream sent
by the client socket.

0
 
LVL 8

Accepted Solution

by:
Cesario earned 100 total points
ID: 6158748
mrk_raj,

sorry for delay ;-)

Before sending the stream send have to send the size of the stream :

 1.  Socket.SendBuff ( MemoryStream.Size,SizeOf(MemoryStream.Size);
2 Send the Stream


The receive the stream you can use this code ( not tested!)


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ScktComp;

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    NewStream  :Boolean;
    Datasize   :LongInt;
    DataStream : tMemoryStream;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

CONST
  STX = 2;
  ETX = 3;

Type
  TMsgHeader = packed Record
     Size : LongInt;
  End;

procedure TForm1.FormCreate(Sender: TObject);
begin
  NewStream := False;
  DataStream := tMemoryStream.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DataStream.Free;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);

Type
  pBuffer = Array[1..65000] OF char;

Var
  Buffer     : ^pBuffer;
  packetSize : LongInt;
  Bytes          : Integer;
begin
  PacketSize := Socket.ReceiveLength;
  GetMem ( Buffer, PacketSize);
  Try
    IF Not NewStream Then
    Begin
      Socket.ReceiveBuf ( DataSize,sizeOf(Integer));
      NewStream := True;
      DataStream.Clear;
    End

    Else
    Begin
      Bytes := Socket.ReceiveBuf( Buffer^[1], PacketSize );
       DataStream.Write ( Buffer^[1], Bytes);
       IF DataStream.Size>=Datasize Then
        Begin
          RichEdit2.Lines.LoadFromStream(DataStream);
          NewStream := False;
        End;
    End;

  Finally
    FreeMem ( Buffer, PacketSize );
  End;
end;

end.
0
 
LVL 1

Expert Comment

by:libdanz
ID: 9253457
How do I know a server has finished sending data when I am using TCPClient.receiveln();
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 151
Using idhttp to login to instagram 2 89
Delphi: ForceDirectory plain function 7 31
update joined tables 2 46
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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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