• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 950
  • Last Modified:

delphi tcp/ip client with onrecieve event

Hey, Im writing a program where i establish a connection between the client  program and the server and then the server sends the data as it comes. I refuse to use a timer to pull the data, i want a onrecieve event that I could use to pull the data. Is anyone aware of such a component? tcpclient from standard delphi 7 doesnt work... and indy(idtcpclient) doesnt have an event.

Tom
0
T0masz
Asked:
T0masz
  • 2
1 Solution
 
RickJCommented:
Look at the ICS components on this page,

http://www.overbyte.be/frame_index.html

I am fairly sure they will do what you want and are free!!
0
 
tobjectpascalCommented:
Both components are threaded if i recall (i still use Delphi 5, had Delphi 7 trial to playh with though)

In other words, they don't have events, linux does not have fd_read, fd_write fd_close, etc etc, when winsock encounters a message it sends the message to the application, your application reads the queue and gets fd_read, the function that deals with it then fires off the even FOnRecv(Self......) at which point your components tells you there's some data waiting.

Linux does not have this, so in order to keep compatability both the tcpclient and the indy don't support none blocking sockets, ie, you have to loop for the data.

Solution
---------

Delphi > Menu > Component > Install Packages

click on "Add"

C:\Program Files\Borland\Delphi7\Bin\dclsockets70.bpl

look for that and install it.

you will end up with a new internet component, ones' a server the other a client.

stick the Client on the form

ClientSocket1.Port:=666;
ClientSocket1.Host:='127.0.0.1';
ClientSocket1.Active:=True;

OnConnect..

procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin

end;


procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
Var
   S: String;
begin
  S:=Socket.ReceiveText;
  ShowMessage(S);
end;


Etc etc, it's the easiest component to use and will always be the easiest, i hate indy.
0
 
T0maszAuthor Commented:
I couldnt install the additional component in my delphi 7

as for ICS I got most of the stuff to work, its almost perfect with one MAJOR problem. I can connect the first time but after disconnecting the server isnt listening on the port agian...

unit Unit1;

interface

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

const
  WM_AUTO_START      = WM_USER + 1;
  WM_CLOSE_REQUEST   = WM_USER + 2;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    WSocket1: TWSocket;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure WSocket1DataAvailable(Sender: TObject; ErrCode: Word);
    procedure Button2Click(Sender: TObject);
    procedure WSocket1SessionAvailable(Sender: TObject; ErrCode: Word);
    procedure WSocket1SessionClosed(Sender: TObject; ErrCode: Word);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function ReplaceChar (const Source : String; const Find, Replace: Char) : string;
var
  i: integer;
  B: PByteArray; // Attention: zero-based!
  Fi, Re: byte;
begin
  Fi := ord (Find);
  Re := ord (Replace);
  Result := Source;
  UniqueString (Result);
  B := @Result [1];
  { i starts at zero, because B is zero-based }
  for i := 0 to length (Result) - 1 do
    if B [i] = Fi then
      B [i] := Re;
end;


function HexToString(S: string): string;
var
  i: integer;
begin
  Result := '';
  for i := 1 to Length(S) do
    if ((i mod 2) = 1) then
      Result := Result + Chr( StrToInt( '0x' + Copy( S, i, 2 )));
end;

function StringToHex(S: string): string;
var
  i: integer;
begin
  Result := '';
  for i := 1 to Length(S) do
    Result := Result + IntToHex( Ord( S[i] ), 2 );
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
wsocket1.proto:='tcp';
wsocket1.port:=Edit1.Text;
wsocket1.addr:='0.0.0.0';
PostMessage(Handle, WM_AUTO_START, 0, 0);

wsocket1.LISTEN;
end;

procedure TForm1.WSocket1DataAvailable(Sender: TObject; ErrCode: Word);
var
  s: string;
  data : string;
begin
     data := Trim(wsocket1.ReceiveStr);
     Memo1.Lines.Add('Recv ASCII: ' + data);
     Memo1.Lines.Add('Recv   HEX: ' + StringtoHex(data));


    if data = 'E' then wsocket1.SendStr(HexToString(StringReplace('00 01 02 49 41 58',' ','',[rfReplaceAll, rfIgnoreCase]))+chr(13));
    if data = 'B' then wsocket1.SendStr(HexToString(StringReplace('54 6F 6D 20 4E 41 20 33 62 61 72 20 42 00 33 30 34 54 33 02',' ','',[rfReplaceAll, rfIgnoreCase]))+chr(13));
    if data = 'G' then wsocket1.SendStr(HexToString(StringReplace('53 31 31 33 30 32 38 30 44 45 31 38 30 30 34 34 30 35 42 39 30 30 33 32 46 41 30 41 37 31 46 46 30 30 41 31 45 36 30 41 33 42 0D 0A',' ','',[rfReplaceAll, rfIgnoreCase]))+chr(13));
    if data = 'A' then wsocket1.SendStr(HexToString(StringReplace('41 58',' ','',[rfReplaceAll, rfIgnoreCase]))+chr(13));
    if data = 'T' then wsocket1.SendStr(HexToString(StringReplace('FF FF FF FF FF FF FF FF FF FF FF FF 41 58',' ','',[rfReplaceAll, rfIgnoreCase]))+chr(13));
    if data = 'F' then wsocket1.SendStr(HexToString(StringReplace(Edit2.Text,' ','',[rfReplaceAll, rfIgnoreCase]))+chr(13));

    if data = Hextostring('00') then wsocket1.SendStr(HexToString(StringReplace('41 58',' ','',[rfReplaceAll, rfIgnoreCase]))+chr(13));

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Wsocket1.SendStr('DUPA');
end;

procedure TForm1.WSocket1SessionAvailable(Sender: TObject; ErrCode: Word);
begin
wsocket1.hsocket:= wsocket1.accept;
end;

procedure TForm1.WSocket1SessionClosed(Sender: TObject; ErrCode: Word);
begin
//PostMessage(Handle, WM_CLOSE_REQUEST, 0, LParam(WSocket1));
PostMessage(Handle, WM_AUTO_START, 0, 0);

end;
end.
0
 
RickJCommented:
In your download you would have the Delphi\Internet directory.
Open the CliDemo and the SrvDemo. Run them both and check code to see how they are communicating between each other.
This is what you are after??
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now