Autor Beitrag
FrEaKY
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 235


D7
BeitragVerfasst: Di 01.02.05 20:08 
Hi,
Also mein Chat funktioniert jetzt so wie ich es haben möchte, zwar mit Timer beim Client anstatt Thread aber das Reicht für die momentanen Zwecke aus. Mein Problem ist, dass sich der Client unmittelbar aufhängt, nachdem er connected ist und vom Server eine Nachricth gekriegt hat. Ich hab da auch schon ein AntiFreeze hingemacht aber das half nix. Die Form ist komplett eingefroren und ich konnte net mehr auf sie zugreifen. Nurnohc Taskmanager :(
Hatte jemand mal ein ähnliches Problem und kann mir helfenm???
Ich poste besser noch meinen sourcecode, vielleicht hab ich was falsch gemachrt.
Server:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdIPWatch, StdCtrls, ComCtrls,
  ExtCtrls, IdTCPServer,Math;

type
  TClient = class(TObject)
    Name: String;
    ListLink: Integer;
    Thread: Pointer;
  end;
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    RichEdit1: TRichEdit;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    CheckBox1: TCheckBox;
    Edit3: TEdit;
    Button1: TButton;
    GroupBox3: TGroupBox;
    ListBox1: TListBox;
    Button2: TButton;
    IdIPWatch1: TIdIPWatch;
    GroupBox4: TGroupBox;
    Label3: TLabel;
    ComboBox1: TComboBox;
    Label4: TLabel;
    Edit4: TEdit;
    Label5: TLabel;
    Panel1: TPanel;
    ColorDialog1: TColorDialog;
    IdTCPServer1: TIdTCPServer;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure Edit3KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure CheckBox1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure IdTCPServer1Connect(AThread: TIdPeerThread);
    procedure IdTCPServer1Execute(AThread: TIdPeerThread);
    procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1; Clients: TList;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Clients := TList.Create;
  IdIPWatch1.Active := True;
  Edit1.Text := IdIPWatch1.LocalIp;
  IdIpWatch1.Active := False;
  ComboBox1.Items.Assign(Screen.Fonts);
  Combobox1.ItemIndex := 0;

  Richedit1.SelText;
  Richedit1.SelAttributes.Name := 'Arial';
  Richedit1.SelAttributes.Color := RGB(160,160,160);
  Richedit1.Lines.Add('// POLFENTEST HAS BEEN STARTED');
end;

procedure TForm1.Button1Click(Sender: TObject);
var Msg:string;
    Dolle,i:Integer;
    List:TList;
begin
  Msg := '['+TimeToStr(now)+']'+Edit4.Text +'(Server): '+ Edit3.Text;
  Richedit1.SelText;
  Richedit1.SelAttributes.Name := Combobox1.Items[Combobox1.ItemIndex];
  Richedit1.SelAttributes.Color := Panel1.Color;
  Richedit1.Lines.Add(Msg);
  Dolle := ColorToRgb(Panel1.COlor);
  Msg := IntToHex(Dolle,6)+';'+ComboBox1.Items[Combobox1.itemindex]+';'+Msg;
  Edit3.Clear;

  if not idTcpServer1.Active then exit;

  List := idTcpServer1.Threads.LockList;
  for i:=0 to List.Count-1 do
    tIdPeerThread(List.Items[i]).Connection.WriteLn(Msg);
  idtcpServer1.Threads.UnlockList;
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin
  If Colordialog1.Execute then
   Panel1.Color := ColorDialog1.Color;
end;

procedure TForm1.Edit3KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = vk_return then
    Button1.Click;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
  begin
    idTcpServer1.DefaultPort := StrToInt(Edit2.text);
    idTcpServer1.Active := True;
    Richedit1.SelText;
    Richedit1.SelAttributes.Name := 'Arial';
    Richedit1.SelAttributes.Color := RGB(160,160,160);
    Richedit1.Lines.Add('// Rolle has been activated');
  end else
  begin
    idTcpServer1.Active := False;
    Richedit1.SelText;
    Richedit1.SelAttributes.Name := 'Arial';
    Richedit1.SelAttributes.Color := RGB(160,160,160);
    Richedit1.Lines.Add('// Rolle has been deactivated');
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Clients.Free;
  idTcpServer1.Active := False;
end;

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var Client: TClient;
begin
  Client := TClient.Create;
  Client.Name := Athread.Connection.Localname;
  Client.ListLink := Listbox1.items.Count;
  Client.Thread := Athread;
  Listbox1.Items.Add(Client.Name);
  Athread.Data := Client;
  Clients.Add(Client);

  Richedit1.SelText;
  Richedit1.SelAttributes.Name := 'Arial';
  Richedit1.SelAttributes.Color := RGB(160,160,160);
  Richedit1.Lines.Add('// '+Client.Name+' has joined');
end;

function HexCharToWert(CharWert:Char):Integer; 
begin 
  case CharWert of
    'A'..'F':Result:=Ord(CharWert)-65+10; 
    else Result:=StrToInt(CharWert); 
  end; // case 
end; // HexCharToWert

function HexToInt(const HexWert:String):Integer; 
var i:Integer; 
    s:String; 
begin 
  Result:=0; 
  s:=UpperCase(HexWert); 
  for i:=1 to Length(s) do if (Ord(s[i]) in [0..47, 58..64, 71..255]) then 
  begin
    Result:=-1;
    Break;
  end; // for i 
  if (Result=0) then for i:=0 to Length(s)-1 do 
    Result:=Result+(HexCharToWert(s[Length(s)-i])*Round(Power(16, i)));
end; // HexToInt

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var Msg:string;
    Client: TClient; colro,nam:string;
begin
  Msg := Athread.Connection.ReadLn;
  Client := Pointer(Athread.Data);

  if not (Msg<>'') then exit;

  colro := Copy(Msg,1,6);
  Delete(Msg,1,7);
  nam := Copy(msg,1,Pos(';',Msg));
  Delete(Msg,1,Pos(';',Msg));
  RichEdit1.SelText;
  RichEdit1.SelAttributes.Color := HexToInt(colro);
  Richedit1.SelAttributes.Name := nam;
  RichEdit1.Lines.Add(Msg);
end;

procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
var Client: TClient;
begin
  Client := Pointer(Athread.Data);
  Clients.Delete(Client.ListLink);
  Listbox1.Items.Delete(listbox1.Items.IndexOf(Client.Name));
  Richedit1.SelText;
  Richedit1.SelAttributes.Name := 'Arial';
  Richedit1.SelAttributes.Color := RGB(160,160,160);
  Richedit1.Lines.Add('// '+Client.Name+' has left');
  Client.Free;
  Athread.Data := nil;
end;

end.

Client:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdIPWatch, StdCtrls, ComCtrls,
  ExtCtrls, IdTCPServer, IdTCPConnection, IdTCPClient,Math,
  IdAntiFreezeBase, IdAntiFreeze;

type
  TClient = class(TObject)
    Name: String;
    ListLink: Integer;
    Thread: Pointer;
  end;
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    RichEdit1: TRichEdit;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    CheckBox1: TCheckBox;
    Edit3: TEdit;
    Button1: TButton;
    GroupBox3: TGroupBox;
    ListBox1: TListBox;
    GroupBox4: TGroupBox;
    Label3: TLabel;
    ComboBox1: TComboBox;
    Label4: TLabel;
    Edit4: TEdit;
    Label5: TLabel;
    Panel1: TPanel;
    ColorDialog1: TColorDialog;
    IdTCPClient1: TIdTCPClient;
    Timer1: TTimer;
    IdAntiFreeze1: TIdAntiFreeze;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure Edit3KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure CheckBox1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure IdTCPClient1Disconnected(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.Assign(Screen.Fonts);
  Combobox1.ItemIndex := 0;

  Richedit1.SelText;
  Richedit1.SelAttributes.Name := 'Arial';
  Richedit1.SelAttributes.Color := RGB(160,160,160);
  Richedit1.Lines.Add('// POLFENTEST HAS BEEN STARTED');
end;

procedure TForm1.Button1Click(Sender: TObject);
var Msg:string;
    Dolle,i:Integer;
begin
  Msg := '['+TimeToStr(now)+']'+Edit4.Text +': '+ Edit3.Text;
  Richedit1.SelText;
  Richedit1.SelAttributes.Name := Combobox1.Items[Combobox1.ItemIndex];
  Richedit1.SelAttributes.Color := Panel1.Color;
  Richedit1.Lines.Add(Msg);
  Dolle := ColorToRgb(Panel1.COlor);
  Msg := IntToHex(Dolle,6)+';'+ComboBox1.Items[Combobox1.itemindex]+';'+Msg;
  Edit3.Clear;

  if not idTcpClient1.Connected then exit;

  idTcpClient1.WriteLn(Msg);
end;

procedure TForm1.Panel1Click(Sender: TObject);
begin
  If Colordialog1.Execute then
   Panel1.Color := ColorDialog1.Color;
end;

procedure TForm1.Edit3KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = vk_return then
    Button1.Click;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
  begin
    idTcpClient1.Port := StrToInt(Edit2.text);
    idTcpClient1.Host := Edit1.Text;
    idTcpClient1.Connect;
  end else
  begin
    idTcpClient1.Disconnect;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  idTcpClient1.Disconnect;
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
   Richedit1.SelText;
   Richedit1.SelAttributes.Name := 'Arial';
   Richedit1.SelAttributes.Color := RGB(160,160,160);
   Richedit1.Lines.Add('// You have been connected');
end;

procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
  Richedit1.SelText;
  Richedit1.SelAttributes.Name := 'Arial';
  Richedit1.SelAttributes.Color := RGB(160,160,160);
  Richedit1.Lines.Add('// You have been disconnected');
end;

function HexCharToWert(CharWert:Char):Integer; 
begin 
  case CharWert of
    'A'..'F':Result:=Ord(CharWert)-65+10; 
    else Result:=StrToInt(CharWert); 
  end; // case 
end; // HexCharToWert

function HexToInt(const HexWert:String):Integer; 
var i:Integer; 
    s:String; 
begin 
  Result:=0; 
  s:=UpperCase(HexWert); 
  for i:=1 to Length(s) do if (Ord(s[i]) in [0..47, 58..64, 71..255]) then 
  begin
    Result:=-1;
    Break;
  end; // for i 
  if (Result=0) then for i:=0 to Length(s)-1 do 
    Result:=Result+(HexCharToWert(s[Length(s)-i])*Round(Power(16, i)));
end; // HexToInt

procedure TForm1.Timer1Timer(Sender: TObject);
var s:string;
    colro,nam:string;
begin
  if not idTcpClient1.Connected then exit;

  try
  s := idTcpClient1.ReadLn('',5);

  if not (s<>'') then exit;

  colro := Copy(s,1,6);
  Delete(S,1,7);
  nam := Copy(s,1,Pos(';',s));
  Delete(S,1,Pos(';',S));
  RichEdit1.SelText;
  RichEdit1.SelAttributes.Color := HexToInt(colro);
  Richedit1.SelAttributes.Name := nam;
  RichEdit1.Lines.Add(s);
  except
  exit;
  end;
end;

end.

Thanx, MFG Freak
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Mi 02.02.05 16:40 
Hallo!

Du führst ein Readln in dem Timer-Event aus. Das Timer-Event erfolgt durch den VCL-Hauptthread, der auch für die Aktualisierung der Form und Bearbeitung der Nachrichten zuständig ist.

ReadLn wartet so lange, bis Daten ankommen. Sprich: Wenn du keine Daten empfängst, tut sich absolut nichts, du kannst keine Eingaben tätigen etc. .

Die Lösung : Du musst das Readln in einen separaten Thread ablegen. Da das mitunter ein wenig kompliziert ist, habe ich hier in der Open-Source-Units Sparte die Unit "SimpleTCP" gepostet, die enthaltenen Komponenten erledigen das Problem, du hast dort dann ein OnInput-Event.

Cu,
Udontknow
FrEaKY Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 235


D7
BeitragVerfasst: Mi 02.02.05 19:33 
Asooo ok thanx