Einzelnen Beitrag anzeigen

NeoLuxembourg

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

[Indy,TCP,Thread,ReadBuffer] While will nicht stopen!

  Alt 22. Jul 2004, 16:16
Hey leude ...

Also, ich versuche seit ein par Tage ein Client - Server programm (genauer gesagt 2 softs, ein Client un ein Server) um eine bassi für ein Gameserver zu erstellen

Mein SC:
Delphi-Quellcode:
unit UMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdAntiFreezeBase, IdAntiFreeze;

 // ------- NETWORK PROTOKOL --------
 // v001
  type
        TNetConfig = record
                data : boolean;
                CMD : string[20];
                SIP : string[16];
                DIP : string[16];
                end;

  // ---------------------------------
  
  type
        TForm1 = class(TForm)
    tcpClient: TIdTCPClient;
    GroupBox1: TGroupBox;
    btnConnect: TButton;
    edtHost: TEdit;
    edtPort: TEdit;
    edtUser: TEdit;
    edtPass: TEdit;
    btnDisconnect: TButton;
    Label1: TLabel;
    Label2: TLabel;
    memLog: TMemo;
    IdAntiFreeze1: TIdAntiFreeze;
    procedure FormDestroy(Sender: TObject);
    procedure btnConnectClick(Sender: TObject);
    procedure btnDisconnectClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  inloop : integer = 1; //default value = 1 (while in thread = stop !)

implementation

{$R *.DFM}

function netscan(P : pointer): integer;
var
        Data:TNetConfig;
begin

// Code !
while inloop =0 do
        begin
          //read data from server

 // PROBLEM IST VON HIER
          Form1.tcpclient.ReadBuffer(Data,sizeof(Data));
 // BIS HIER ;)

          if Data.data = false then
                Form1.memLog.Lines.add('STR:'+Data.CMD)
          else
                Form1.memLog.Lines.add('CMD:'+Data.CMD);
                
          // read thread status (inloop value)
          sleep(1);
          Form1.Label1.Caption := inttostr(inLoop);

        end;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
        //set inLoop to 1 (stops while in thread)
        InterlockedExchange(inLoop, 1);
        // TCP Disconnection !
        tcpclient.Disconnect;
end;

procedure TForm1.btnConnectClick(Sender: TObject);
var
  ThreadID: LongWord;
  p: Pointer;
begin

   // Server-IP (Data from edtHost)
        tcpclient.Host := edtHost.Text;
        // Server-Port (Data from edtPort)
        tcpclient.Port := strtoint(edtPort.Text);
        // Connection to server
        tcpClient.Connect;
        // If connection is succesfull, then start the thread !
        if tcpClient.Connected then
        begin
                memLog.Lines.Add('Connected to server!');
                //set inLoop to 0 (while in thread can run)
                InterlockedExchange(inLoop, 0);
                //start thread !
                CloseHandle(BeginThread(nil, 0, @netscan, p, 0, ThreadID));
        end;
end;

procedure TForm1.btnDisconnectClick(Sender: TObject);
begin
    //set inLoop to 1 (stops while in thread)
    InterlockedExchange(inLoop, 1);
end;

end.
Main problem:

Wenn ich die zeile

Form1.tcpclient.ReadBuffer(Data,sizeof(Data)); setze dann kann ich den wert von inLoop nich mehr ändern. Es bleibt immer bei 0, dh das mein while schleiffe im thread nicht stopt und wenn ich das Programm einfach zuclicke et halte ich eine Fehlermeldung ... execption .. bla bla .."Disconnect" ... Ich verstehe wo das problem ist ... ich versuche ein ReadBuffer() zu machen, doch ich bin schon disconnected ... doch? Wie löse ich das jetzt?

- Thread killen?

Warum geet das mit dem inLoop nicht ?

Thx

PS: Hab den SC so gut es nur geht geComment!
  Mit Zitat antworten Zitat