Autor Beitrag
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Sa 01.11.03 20:40 
Hi!

ich hab hier irgendwie n blödes problem, das ich nich gelöst bekomm...

also ich erzeug in einer Funktion von FrmMain lokal einen TCPServer (TIdTCPServer; Indy). und zwar so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TFrmMain.TCPServerExecute(AThread: TIdPeerThread);
var TTCPClient:TIdTCPClient;
    TCPThread:TTCPThread;
    TCPClient_number:integer;
[..]
begin
  [..]
  TCPClient:=TIdTCPClient.Create(Self);
  [..]

dann (gleiche Funktion) hau ich nen Pointer von diesem TCPClient in eine TThreadlist:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
  try
    with TCPClients.LockList do
    begin
      Add(Pointer(TCPClient));  // hier wird der Pointer dazugefügt
      TCPClient_number:=Count-1;  // die stelle an der der Pointer eingefügt wird (das braucht nacher der Thread)
    end;
  finally
    TCPClients.UnlockList;
  end;
  [..]

und dann start ich nen Thread (immer noch gleiche Funktion):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
  TCPThread:=TTCPThread.Create(True);
  TCPThread.FreeOnTerminate:=true;
  TCPThread.TCPClient_number:=TCPClient_number;  // Stelle in der TCPClient-Liste merken
  [..]
  TCPThread.Resume;  // Thread starten
  [..]
  TCPClient.Write(request);
end;

soweit geht noch alles. kurz danach schick ich ja noch ne Message mit dem TCPClient, der is also ansprechbar (ob die Message jetzt ankommt weiß ich net, aber das is jetzt au erstmal egal); es gibt auf jeden Fall keine Access Violation.

So. Und wenn der Thread gestartet is, dann tut der das (andere Unit):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
[...]
TCPClient:^TIdTCPClient;  // ich hab ja in der Liste nen Pointer...
[..]

procedure TTCPThread.Execute;
begin
  try
    TCPClient:=TCPClients.Locklist.Items[FTCPClient_number];  // hier ließt er den Pointer vom TCPClient aus der Liste aus
  finally
    TCPCLients.UnlockList;
  end;
  while not Terminated do  // solange der Thread noch nich ge"terminated" wurde..
  begin
    if not TCPClient.Connected then  // <========= HIER
    [..]
end;

Und an der gekennzeichneten Stelle bekomm ich ne AccesViolation... :( Also dieser TCPCLient is ne lokale Variable von TCPServerExecute, soll allerdings nacher in dem Thread-Execute verfügbar sein. wie bekomm ich das hin? Habs ja, wie gesehen mit ner globalen Liste, die nen Pointer beinhaltet versucht, aber geht net :( also ich geb den TCPClient am ende der TCPServerExecute natürlich NICHT frei, das soll nacher auch das Thread-Execute übernehmen... Aber wie bekomm ich das hin? ich will TCPClient net Global deklarieren, da ich net bloß einen TCPClient brauch, sondern mehrere und zwar dynamisch...



MrSaint

P.S.: Soll am Ende n Proxy-Server für HTTP werden, deswegen Server und Client Komponenten. Und die kommunikation soll ungefähr so aussehn:

Client <===> [ TCPClient <===> TCPServer ] <===> Web-Server

und das in den [ ] is mein programm... ich hoff das war jetzt net zu unverständlich ;)

Moderiert von user profile iconMrSaint: Titel des Topics geändert
Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems


Zuletzt bearbeitet von MrSaint am Di 04.11.03 11:36, insgesamt 1-mal bearbeitet
MrSaint Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Di 04.11.03 11:36 
Problem wurde gelöst!

Es muss so heißen:

ausblenden Quelltext
1:
2:
3:
[...] 
TCPClient:TIdTCPClient;  // NIX POINTER, IS SCHO EINER!
[..]


und so:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
  try 
    with TCPClients.LockList do 
    begin 
      Add(TCPClient);  // hier wird der Pointer dazugefügt ; OHNE Pointer(
      TCPClient_number:=Count-1;  // die stelle an der der Pointer eingefügt wird (das braucht nacher der Thread) 
    end; 
  finally 
    TCPClients.UnlockList; 
  end; 
  [..]



weil:

OnlineHelp hat folgendes geschrieben:
Classes and objects

A class, or class type, defines a structure consisting of fields, methods, and properties. Instances of a class type are called objects. The fields, methods, and properties of a class are called its components or members.

A field is essentially a variable that is part of an object. Like the fields of a record, a class’s fields represent data items that exist in each instance of the class.
A method is a procedure or function associated with a class. Most methods operate on objects—that is, instances of a class. Some methods (called class methods) operate on class types themselves.
A property is an interface to data associated with an object (often stored in a field). Properties have access specifiers, which determine how their data are read and modified. From other parts of a program—outside of the object itself—a property appears in most respects like a field.

Objects are dynamically allocated blocks of memory whose structure is determined by their class type. Each object has a unique copy of every field defined in the class, but all instances of a class share the same methods. Objects are created and destroyed by special methods called constructors and destructors.
A variable of a class type is actually a pointer that references an object. Hence more than one variable can refer to the same object. Like other pointers, class-type variables can hold the value nil. But you don’t have to explicitly dereference a class-type variable to access the object it points to. For example, SomeObject.Size := 100 assigns the value 100 to the Size property of the object referenced by SomeObject; you would not write this as SomeObject^.Size := 100.


Muss zugeben, dass mir das bisher noch nich bekannt war :oops: (so wurde mir das in nem anderen Forum gesagt und so geht's auch)


MrSaint

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems