Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sending formatted text over a socket connection

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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