Solved

Sending formatted text over a socket connection

Posted on 2001-06-04
4
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Internet Explorer View Settings Question 15 123
error 1.1 400 Bad request idhttp delphi 18 206
creating threads in delphi 1 163
DBCtrlGrid, Delphi, Scroll 8 59
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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