Link to home
Create AccountLog in
Avatar of TracerXXX
TracerXXX

asked on

Delphi TCPServer / TCPClient

I need an example of a simple TCPServer / TCPClient application for Delphi 2009. I know this has been asked before but no one ever answers the question. Either the asker is pointed to Indy (where examples are outdated, i.e. Delphi 7) or are missing components. Please point me to an example using the components TTCPServer and TTCPClient available in Delphi 2009. I will be very grateful. Thanks.
SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
@jimyX

Just curious
Why is TServer/TClient Socket better that TTCPServer/TTCPClient
know that there is a lack of INDY 10 samples . that why I made this sample ....

http://sourceforge.net/projects/indy10clieservr/ 

*  still there is a small bug inside  for the record fumction ... solution is here at EE
Avatar of jimyX
jimyX

Hi ewangoya,

My personal experience, I have always find the TServer/TClient Socket Components are very handy and you can rely on. I realized that when I started on a network project which had to do Client/Server communication so I got the chance to try them all, and I was convinced with the TServer/TClient Socket in terms of performance and easy programming.

Also you can read the Description part of this link:
http://qc.embarcadero.com/wc/qcmain.aspx?d=68811
hi guys
I have much to learn from you just listening.

Happy New Year to all,

Epasquier is not Active anymore? maybe a stomach ache. lol
Hi Systan,
He has been inactive since Christmas. If it was stomach ache he would here by now, I think he is still enjoying foods/drinks :-)

Happy New Year to you too.

Regards.
@Systan
Happy new year to you

@jimmyX
Got it

I'll write an article/tutorial and sample application on TServer/TClient Socket sometime this month. That should be useful to the community
That was my idea actually, since sometime back I am working on it, but if you got yours ready then go for it, I will be happy for you.

jimmyX
I haven't started yet, just thought of it. Don't let your work go to waste.
No need for me to duplicate an article, so just get yours ready and submit
Avatar of TracerXXX

ASKER

jimmyX

Thanks for your reply. I took your socket code to see if I could get the simplest of programs to work. I'm very close but the data sent to the server ends up corrupted. The ShowMessage dialog displays "Hello to" follow by garbage. Am I pasiing the pointer to the buffer properly? Below is my full source (one app for both client and server).

//-----------------------------------------------------------------------------

unit EMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Sockets, StdCtrls, Buttons, ScktComp;

type
  TForm2 = class(TForm)
    btnServer: TButton;
    btnClient: TButton;
    ClientSocket1: TClientSocket;
    ServerSocket1: TServerSocket;
    btnSend: TButton;
    procedure btnServerClick(Sender: TObject);
    procedure btnClientClick(Sender: TObject);
    procedure btnSendClick(Sender: TObject);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2     : TForm2;
  bIsServer : Boolean;

implementation

{$R *.dfm}

procedure TForm2.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  Bfr   : PChar;
begin
  Bfr:=PChar(Socket.ReceiveText);
  ShowMessage(Bfr);
end;

//-----------------------------------------------------------------------------
procedure TForm2.btnClientClick(Sender: TObject);
begin
  ClientSocket1.Address:='xx.xxx.xxx.xxx';
  ClientSocket1.Port:=1212;
  ClientSocket1.Active:=True;

  btnSend.Visible:=True;
  btnServer.Visible:=False;
  btnClient.Visible:=False;
end;

//-----------------------------------------------------------------------------
procedure TForm2.btnServerClick(Sender: TObject);
begin
  ServerSocket1.Port:=1212;
  ServerSocket1.Active:=True;

  btnServer.Visible:=False;
  btnClient.Visible:=False;
end;

//-----------------------------------------------------------------------------
procedure TForm2.btnSendClick(Sender: TObject);
var
  Bfr: PChar;
begin
  Bfr := PChar('Hello to Server');
  ClientSocket1.Socket.SendBuf(Bfr^, length(Bfr));
end;

end.
ReceiveText has an issue in Delphi 2009 so use ReceiveBuf instead:

 
procedure TForm_Server.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
  iLen: Integer;
  Bfr: PChar;
  StrStrm : TStringStream;
begin
  StrStrm := TStringStream.Create;
  iLen := Socket.ReceiveLength;
  GetMem(Bfr, iLen);
  try
    Socket.ReceiveBuf(Bfr^, iLen);
    StrStrm.Write(Bfr^, iLen);
    ShowMessage((StrStrm.DataString);
  finally
    StrStrm.Free;
  end;
end;

Open in new window

Please ignore my last post #34469138 I was commenting on the wrong side of the problem. It is not about receiving here it is about sending.

This is really odd if you read that same string from EditBox and send it, then it will be transmitted correctly.

Try this:
 
procedure TForm2.btnSendClick(Sender: TObject);
var
  Bfr: PChar;
begin
  Edit1.Text := 'Hello to Server';
  Bfr := PChar(Edit1.Text);
  ClientSocket1.Socket.SendBuf(Bfr^, length(Edit1.Text));
end;

Open in new window


I will be after finding out why is that.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks. The Delphi unicode changes still trip me up occasionally. Once I changed the buffer to a PAnsiChar everything worked as expected.