?
Solved

Sending formatted text over a socket connection

Posted on 2001-06-04
4
Medium Priority
?
431 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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