Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi 2009: TTCPServer and TTCPConnect

Status
Not open for further replies.

towerbase

Programmer
Jul 31, 2002
1,053
0
0
GB
I wish to transfer a small amount of data from one Win32 exe to another Win32 exe. The code I've written uses TTCPServer and TTCPConnect components. It works when compiled using Delphi 7 but fails when compiled using Delphi 2009. In both cases I've run the programs on Windows XP machines.

To demonstrate the problem, I've coded two programs. One called Server and the other called Client.

Here is the code for Client:
Code:
unit ClientMain;

interface

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

type
  TForm1 = class(TForm)
    Client: TTcpClient;
    btnConnect: TButton;
    btnSend: TButton;
    edText: TEdit;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure btnSendClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnConnectClick(Sender: TObject);
begin
  Client.Disconnect;
  Client.RemoteHost := '127.0.0.1';
  Client.RemotePort := '2609';
  Client.Connect;
  if Client.Connected then
    Form1.Caption := 'Client Connected';
end;

procedure TForm1.btnSendClick(Sender: TObject);
begin
  if Client.Connected then
    Client.SendLn( 'abcdefghijklmnop' );
end;

end.

This is the code for Server
Code:
unit ServerMain;

interface

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

type
  TForm1 = class(TForm)
    Server: TTcpServer;
    btnConnect: TButton;
    log: TMemo;
    procedure btnConnectClick(Sender: TObject);
    procedure ServerAccept(Sender: TObject; ClientSocket: TCustomIpClient);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnConnectClick(Sender: TObject);
begin
  Server.Active := false;
  Server.LocalHost := '127.0.0.1';
  Server.LocalPort := '2609';
  Server.Active := true;
  if Server.Active then
    Form1.Caption := 'Server Connected';
end;

procedure TForm1.ServerAccept(Sender: TObject;
  ClientSocket: TCustomIpClient);
var
  s: string;
begin
//  log.Lines.Append( ClientSocket.LookupHostName( ClientSocket.RemoteHost ) );
  s := ClientSocket.ReceiveLn;
  while s <> '' do begin
    log.Lines.Append( s );
    s := ClientSocket.ReceiveLn;
  end;
end;

end.
Each program is started. The Connect button is clicked on the Server application, the Connect button is clicked on the Client application and then the Send button is clicked on the Client application.

When compiled under Delphi 7, the expected text appears in the memo.

When compiled under Delphi 2009, nothing appears in the memo. However, if I click on Connect again in the Client application abcdefghiabcdefghi appears in the memo.

Is anyone aware of problems with the Delphi 2009 TTCPClient and TTCPServer components and if there is a fix?

Andrew
 
This is a Unicode problem.

Code:
s := ClientSocket.ReceiveLn;

S you have declared as a String. I've never used the TTCPServer so I don't know what ReceiveLn returns, but I assume you need to properly convert this data to a string, due to the forced Unicode in later versions of Delphi.


JD Solutions
 
Also, are you sure you should use the "Accept" event? Maybe there should be another event like "OnClientRead" (at least that's how it is with TServerSocket) because Accept I would assume is only when the server "Accepts" a connection, not necessarily when the server receives data.

JD Solutions
 
Thank you for your speedy response.

I based my program on some sample code I found on the web. Typically, there was no indication of which version of Delphi it was developed with. So

However, as it works perfectly with Delphi 7, your suggestion that it is a Unicode issue seems more plausible. I'll investigate further.

Andrew
 
From D2009 dealing with this sort of situations you need RawByteString. if these are strings you are sending you know what encoding to apply...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top