Einzelnen Beitrag anzeigen

Beckman

Registriert seit: 12. Jul 2004
205 Beiträge
 
#1

problem mit VCL port -> nonVCL (tcp)

  Alt 21. Feb 2005, 22:15
//communikation mit GUI/client funkt, mit cmdline/client net.

also geschickt werden die texte, aber empfangen wird einfach nix.
server ist allerdings in ordnung, weil beim gui/clienten funkt es ja...

die loesung hab ich irgendwo im net gefunden fuer den indy tcpclienten, nur porten tuts net so richtig :/

jo, ich brauchs cmdline.


waer dankbar fuer hilfe.....



GUI/client:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ScktComp, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Memo1: TMemo;
    Edit3: TEdit;
    TCP: TIdTCPClient;
    procedure Edit3KeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure SendLine(Src: String);
  end;

  TClientHandleThread = class(TThread)
  private
    Message: String;
    procedure HandleInput;
  protected
    procedure Execute; override;
  end;

var
  Form1: TForm1;
  ClientHandleThread: TClientHandleThread;

implementation

{$R *.dfm}

procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if (key = #13) then begin
  memo1.lines.add('OUT: ' + edit3.Text);
  SendLine(edit3.text);
  edit3.clear;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
tcp.Disconnect;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if tcp.Connected then begin
  memo1.lines.add('already connected');
  exit;
  end;
TCP.Host := edit1.Text;
TCP.Port := strtoint(edit2.Text);
try
  TCP.Connect(3000);
  ClientHandleThread := TClientHandleThread.Create(True);
  ClientHandleThread.FreeOnTerminate:=True;
  ClientHandleThread.Resume;
except
  showmessage('error!');
  end;
end;

procedure TForm1.SendLine(Src: String);
begin
tcp.WriteInteger(length(Src));
tcp.Write(src);
end;

procedure TClientHandleThread.HandleInput;
begin
form1.memo1.lines.add('IN: ' + Message);
end;

procedure TClientHandleThread.Execute;
begin
  while not Terminated do
  begin
    if not Form1.TCP.Connected then
      Terminate
    else
    try
      Message := Form1.TCP.ReadString(Form1.TCP.ReadInteger);
      Synchronize(HandleInput);
    except
    end;
  end;
end;

end.






CMDLINE/client:


Delphi-Quellcode:
program becmac;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows, IdTCPClient, Classes;

type
  TClientHandleThread = class(TThread)
  private
    Message: String;
    procedure HandleInput;
  protected
    procedure Execute; override;
  end;

var
  TCP: TIdTCPClient;
  ClientHandleThread: TClientHandleThread;

procedure SendLine(Src: String);
begin
tcp.WriteInteger(length(Src));
tcp.Write(src);
end;

procedure TClientHandleThread.HandleInput;
begin

writeln('IN: ' + Message);

end;

procedure TClientHandleThread.Execute;
begin
  while not Terminated do
  begin
    if not TCP.Connected then
      Terminate
    else
    try
      Message := TCP.ReadString(TCP.ReadInteger);
      Synchronize(HandleInput);
    except
    end;
  end;
end;

begin

TCP := TIdTCPClient.Create(nil);
ClientHandleThread := nil;
TCP.Host := <HOST>;
TCP.Port := <PORT>;
try
  TCP.Connect(5000);
  ClientHandleThread := TClientHandleThread.Create(True);
  ClientHandleThread.FreeOnTerminate := True;
  ClientHandleThread.Resume;
except
  ClientHandleThread.Free;
  TCP.Free;
  writeln('Connection failed...');
  Exit;
  end;

-----------------------------------



//da hat er halt schleifen wo er wartet, bis gewisse strings gekommen sind
//mit sleep(1)




-----------------------------------




//exit
Sendline('BYE');
ClientHandleThread.Free;
TCP.Free;

end.
  Mit Zitat antworten Zitat