AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi Normale TCP connection mit Indy10
Thema durchsuchen
Ansicht
Themen-Optionen

Normale TCP connection mit Indy10

Ein Thema von Jamah · begonnen am 20. Mai 2010 · letzter Beitrag vom 21. Mai 2010
Antwort Antwort
Jamah

Registriert seit: 2. Dez 2009
Ort: Nordhorn
74 Beiträge
 
RAD-Studio 2010 Arc
 
#1

Normale TCP connection mit Indy10

  Alt 20. Mai 2010, 18:25
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
  Mit Zitat antworten Zitat
Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.755 Beiträge
 
Delphi 10.4 Sydney
 
#2

Re: Normale TCP connection mit Indy10

  Alt 20. Mai 2010, 21:01
Guten Abend,

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

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

AContext.Connection.IOHandler.Write('...'); Grüße
Klaus
Klaus
  Mit Zitat antworten Zitat
Jamah

Registriert seit: 2. Dez 2009
Ort: Nordhorn
74 Beiträge
 
RAD-Studio 2010 Arc
 
#3

Re: Normale TCP connection mit Indy10

  Alt 20. Mai 2010, 21:52
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?
  Mit Zitat antworten Zitat
Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.755 Beiträge
 
Delphi 10.4 Sydney
 
#4

Re: Normale TCP connection mit Indy10

  Alt 21. Mai 2010, 07:53
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 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 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
Klaus
  Mit Zitat antworten Zitat
Jamah

Registriert seit: 2. Dez 2009
Ort: Nordhorn
74 Beiträge
 
RAD-Studio 2010 Arc
 
#5

Re: Normale TCP connection mit Indy10

  Alt 21. Mai 2010, 12:54
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
  Mit Zitat antworten Zitat
Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.755 Beiträge
 
Delphi 10.4 Sydney
 
#6

Re: Normale TCP connection mit Indy10

  Alt 21. Mai 2010, 14:24
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
Klaus
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:42 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