Solved

Sending formatted text over a socket connection

Posted on 2001-06-04
4
411 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
How do I know a server has finished sending data when I am using TCPClient.receiveln();
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

13 Experts available now in Live!

Get 1:1 Help Now