Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi Normale TCP connection mit Indy10 (https://www.delphipraxis.net/151515-normale-tcp-connection-mit-indy10.html)

Jamah 20. Mai 2010 18:25


Normale TCP connection mit Indy10
 
Tag leute.
Ich werde hier mal wieder nicht schlauer....
Ich möchte eine Verbindung (TCP) zwischen einem Client und einem Server herstellen (geht), über diese Verbindung Strings hin schieben (quasi befehlsanweisungen) und dann einen String vom Server zurückkommen lassen. Der muss ja rückmeldung erstatten.
Das geht nicht...

Ich hab mit einen IDTCPServer und einen IdTCPClient gemacht.
Dann einfach Host und Port gesetzt.

Delphi-Quellcode:
procedure TForm1.SendClick(Sender: TObject);
 var
  sendStr: String;
begin
 SendStr:= (Edit1.Text+'|'+Edit2.Text+'|'+Edit3.Text);
 if not IdTCPClient1.Connected then
  begin
   IdTCPClient1.Connect;
  end;
 Label2.Caption:= BooltoStr(IdTCPClient1.Connected);
 IdTCPClient1.IOHandler.WriteLn(SendStr);
end;
Geht auch.

Dann am Server:

Delphi-Quellcode:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
 begin
  IdTCPServer1.Active:= True;
  ListBox1.Items.Add(split(AContext.Connection.IOHandler.ReadLn(),'|',1));
 end;
Würde das theoretisch auch weiter ausgebaut funktionieren? (Es soll irgendwann ein Chat werden (nur eine Verbindung) quer durchs Netz. Also nicht in einem internen Netzwerk oder so.

Und wie bekomme ich den Server jetzt dazu, zu antworten?

Danke

Klaus01 20. Mai 2010 21:01

Re: Normale TCP connection mit Indy10
 
Guten Abend,

in dem Ereignis onExecute bekommst Du als Paramter AContext übergeben.

Um an den Client etwas zu schicken kannst Du AContext hernehmen.

Delphi-Quellcode:
AContext.Connection.IOHandler.Write('...');
Grüße
Klaus

Jamah 20. Mai 2010 21:52

Re: Normale TCP connection mit Indy10
 
Ok. Danke.
Aber wie baue ich das ein? Also wenn ich das in die Prozedur mache, wo der client was sendet, und dann der server was zurückgibt, bleibt das Porgramm stehen.

Und:
Was mache ich, wenn ich nicht aus dem Kontext sondern aus irgendeiner Prozedur eine Rückgabe brauche?

Klaus01 21. Mai 2010 07:53

Re: Normale TCP connection mit Indy10
 
Zitat:

Zitat von Jamah
Ok. Danke.
Aber wie baue ich das ein? Also wenn ich das in die Prozedur mache, wo der client was sendet, und dann der server was zurückgibt, bleibt das Porgramm stehen.

Ich kann mir schwer vorstellen das es hängt, bur weil Du etwas zum Client schickst.

Ich denke es hängt, weil readln auf Daten wartet.
Zitat:

Zitat von DelphiHilfe

ReadLn is an overloaded string function used to read a single line of text from values received from the peer connection for the IOHandler.

ReadLn is a String function that returns a a single line from the input buffer maintained for the IOHandler. This variant of the ReadLn method uses the LF character as the end-of-line delimiter, and calls an overloaded variant of the method.

ReadLn retrieves data from InputBuffer until the end-of-line sequence is located, the maximum line length is exceeded, the socket connection is closed, or a timeout occurs. If there is not enough data to satisfy the request, ReadLn calls CheckForDisconnect to update the IOHandler status and reads data from the data source for the IOHandler connection.

ReadLn will return an empty string ('') if the peer connection is prematurely closed or a time-out occurs. If a timeout condition occurs in ReadLn, the ReadLnTimedOut property is set to True.

ReadLn can raise an EIdReadLnMaxLineLengthExceeded exception when the position of the end-of-line sequnce in the InputBuffer is greater than the maximum line length allowed for an IOHandler using the value maException in MaxLineAction.

All ATerminator characters in the return value for ReadLn (including CR and LF characters) are removed prior to exiting from the method.

Zitat:

Zitat von Jamah
Und:
Was mache ich, wenn ich nicht aus dem Kontext sondern aus irgendeiner Prozedur eine Rückgabe brauche?

Wenn Du es nicht aus dem Context heraus etwas schicken willst,
dann mußt Du die ThreadListe des Servers durchgehen und den Clients die in der Liste stehen
etwas schicken. (Oder Du führtst selber eine Liste und nutzt diese).
Delphi-Quellcode:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
  IdTCPServer1.Active:= True;
  ListBox1.Items.Add(split(AContext.Connection.IOHandler.ReadLn(),'|',1));
end;
Es ist etwas ungeschickt, den Sever im onExecute zu aktivieren.
Wäre der Server da nicht schon bereits aktiviert würde das onExecute auch
nicht ausgelöst werden.

Grüße
Klaus

Jamah 21. Mai 2010 12:54

Re: Normale TCP connection mit Indy10
 
Ok. Das erschließt sich mir jetzt so weit ganz gut.
Einzige Frage, die noch bleibt:
gibt es eine prozedur wie 'IdTCPServer1.OnReadLn' so? Also eine, die ausgeführt wird, wenn was reinkommt?

Danke

Klaus01 21. Mai 2010 14:24

Re: Normale TCP connection mit Indy10
 
Zitat:

Zitat von Jamah
..
gibt es eine prozedur wie 'IdTCPServer1.OnReadLn' so? Also eine, die ausgeführt wird, wenn was reinkommt?
Danke

Nein, ein onRead gibt es nicht.
Aber die Routine onExecute läuft ja immer im Kreis solange
wie der Client noch verbunden ist.

Was Du machen kannst ist, im onExecute überprüfen wie der Buffer gefüllt ist.

Delphi-Quellcode:
if not AContext.Connection.IoHandler.InputBufferIsEmpty then
  begin
     // mache etwas mit dem Inhalt des Buffers
  end;
Grüße
Klaus


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:26 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz