Autor Beitrag
Killmag10
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 297

Suse Linux / DOS / Windows All In One ;)
D4 / D2005
BeitragVerfasst: Fr 07.09.07 10:43 
Hi

Ich habe eine Server und eine Client Anwendung die aus der Ferne ein Programm neustarten kann.

Das Problem der Server funzt 1A blos der Client meint das er bereits verbunden ist wenn ich ihn ein zweites mahl benutze, wenn ich den client neustarte geht es wieder.

Ihrgentwie scheint Indy auf den Client nicht mit zu bekommen das die Verbindung geschlossen wurde, der Server aber erkennt das die Verbindung geschlossen wurde.

Was mache ich falsch ?

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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, Sockets, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient;

type
  TForm1 = class(TForm)
    edit_ip: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    edit_password: TMaskEdit;
    IdTCPClient1: TIdTCPClient;
    procedure IdTCPClient1Disconnected(Sender: TObject);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if edit_password.Text='' then exit;

  Button1.Enabled:=false;
  IdTCPClient1.Port:=7999;
  IdTCPClient1.Host:=edit_ip.Text;
  IdTCPClient1.Connect;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  IdTCPClient1.Disconnect;
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
var
  TCPData,showmsg:String;
begin
  showmsg:='';

  if IdTCPClient1.Connected then begin
    IdTCPClient1.IOHandler.WriteLn('restart'+#13+edit_password.Text+#13);

    TCPData:=IdTCPClient1.IOHandler.ReadLn(#13,10000);

    if TCPData='restart_ok' then showmsg:='SERVER RESTARTS !';
    if TCPData='pw_false' then showmsg:='Password false !';

    //IdTCPClient1.IOHandler.WriteLn('close'+#13);
    IdTCPClient1.Disconnect;
  end;

  Button1.Enabled:=true;

  if not(showmsg=''then showmessage(showmsg);
end;

procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
  Button1.Enabled:=true;
end;

end.


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:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdSimpleServer, ShellApi, IdTCPServer, IdContext, IdThread, Sockets, IniFiles, Tlhelp32;

type
  TForm1 = class(TForm)
    anz: TMemo;
    IdTCPServer1: TIdTCPServer;
    procedure anzChange(Sender: TObject);
    procedure IdTCPServer1ListenException(AThread: TIdListenerThread;
      AException: Exception);
    procedure IdTCPServer1Exception(AContext: TIdContext;
      AException: Exception);
    procedure IdTCPServer1Disconnect(AContext: TIdContext);
    procedure IdTCPServer1Connect(AContext: TIdContext);
    procedure FormDestroy(Sender: TObject);
    procedure IdTCPServer1Execute(AContext: TIdContext);
    // ich
    procedure sc_restart;
    // ich
    procedure FormCreate(Sender: TObject);
  private
    tcp_buffer:string;
    login_password,sc_filename:String;
    apppath:string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure KillProcess(dwProcID: DWORD);
var  
  hProcess : Cardinal;  
  dw       : DWORD;  
begin  
  { open the process and store the process-handle }  
  hProcess := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, dwProcID);  
  { kill it }  
  TerminateProcess(hProcess, 0);  
  { TerminateProcess returns immediately, so wie have to verify the result via  
    WaitfForSingleObject }
  
  dw := WaitForSingleObject(hProcess, 5000);  
  case dw of  
    { everythings's all right, we killed the process }  
    WAIT_OBJECT_0: Messagebox(Application.Handle, 'Prozess wurde beendet.''Prozess beenden',  
      MB_ICONINFORMATION);  
    { process could not be terminated after 5 seconds }  
    WAIT_TIMEOUT:  
    begin  
      Messagebox(Application.Handle, 'Es konnte innerhalb von fünf Sekundnen ein Prozeß nicht beendet werden!',  
        'Prozess beenden', MB_ICONSTOP);  
      CloseHandle(hProcess);  
      exit;  
    end;  
    { error in calling WaitForSingleObject }  
    WAIT_FAILED:  
    begin  
      RaiseLastOSError;   
      CloseHandle(hProcess);  
      exit;  
    end;  
  end;   
  CloseHandle(hProcess);  
  { and refresh listbox contend }  
end;


function KillTask(ExeFileName: string): Integer;  
const  
  PROCESS_TERMINATE = $0001;  
var  
  ContinueLoop: BOOL;  
  FSnapshotHandle: THandle;  
  FProcessEntry32: TProcessEntry32;  
begin
// uses Tlhelp32
  Result := 0;  
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);  
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);  

 
  while Integer(ContinueLoop) <> 0 do  
  begin  
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =  
      UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =  
      UpperCase(ExeFileName))) then  
      Result := Integer(TerminateProcess(  
                        OpenProcess(PROCESS_TERMINATE,  
                                    BOOL(0),  
                                    FProcessEntry32.th32ProcessID),  
                                    0));  
     ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);  
  end;  
  CloseHandle(FSnapshotHandle);  
end;

procedure TForm1.sc_restart;
begin

if FileExists(apppath+sc_filename) then begin
   KillTask(sc_filename);
   ShellExecute(Application.Handle, 'open', PChar(apppath+sc_filename), NilNil, SW_NORMAL);
end else anz.Lines.Add('ERORR NO FILE '+apppath+sc_filename);

end;

procedure TForm1.FormCreate(Sender: TObject);
var
 ini:TIniFile;
begin
  tcp_buffer:='';
  apppath:=ExtractFilePath(ParamStr(0));

  Randomize;

  ini:=TIniFile.Create(ChangeFileExt(Application.ExeName, '.INI' ));
  if not FileExists(ini.FileName) then begin
    ini.WriteString('shoutcast','filename_sc','sc_serv.exe');
    ini.WriteString('login','password',inttostr(Random(99999999)));
  end;
  sc_filename:=ini.ReadString('shoutcast','filename_sc','sc_serv.exe');
  login_password:=ini.ReadString('login','password',inttostr(Random(99999999)));
  ini.Free;

  IdTCPServer1.Active:=true;

  if not FileExists(apppath+sc_filename) then anz.Lines.Add('ERORR NO FILE '+apppath+sc_filename);
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
  TCPData:TStringList;
  line,tmp:String;
  OutOfBuffer:Boolean;
  i:integer;
begin
  tcp_buffer:=Acontext.Connection.IOHandler.ReadLnWait(1000);// .ReadString(Acontext.Connection.IOHandler.RecvBufferSize);

// + Erstellung
  TCPData:= TStringList.Create;
  OutOfBuffer:=false;
// - Erstellung

  TCPData.Text:=tcp_buffer;
  tcp_buffer:='';

  while ((TCPData.Count>0and not(OutOfBuffer)) do begin

    anz.Lines.Add('Read');
    //anz.Lines.Add(TCPData.Text);

    line:=TCPData[0];
    if line='close' then begin
      Acontext.Connection.Disconnect;
      TCPData.Delete(0);
    end else if line='restart' then begin
      if TCPData.Count>1 then begin
        if login_password=TCPData[1then begin
          anz.Lines.Add('Shoutcast restart');
          sc_restart;
          Acontext.Connection.IOHandler.WriteLn('restart_ok');
        end else begin
          anz.Lines.Add('BAD PASSWORD');
          Acontext.Connection.IOHandler.WriteLn('pw_false');
        end;
        for i:=1 to 2 do TCPData.Delete(0);
      end else OutOfBuffer:=true;

    end else begin
      TCPData.Delete(0);
    end;

  end;

if OutOfBuffer then tcp_buffer:=TCPData.Text;

  //Acontext.Connection.Disconnect;

// + Freigaben
  TCPData.Free;
// - Freigaben
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  IdTCPServer1.Active:=false;
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
  anz.Lines.Add('OPEN '+AContext.Binding.PeerIP);
end;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
begin
  anz.Lines.Add('CLOSE '+AContext.Binding.PeerIP);
end;

procedure TForm1.IdTCPServer1Exception(AContext: TIdContext;
  AException: Exception);
begin
  anz.Lines.Add('ERORR: '+AException.Message);
end;

procedure TForm1.IdTCPServer1ListenException(AThread: TIdListenerThread;
  AException: Exception);
begin
  anz.Lines.Add('ERORR LISTEN: '+AException.Message);
end;

procedure TForm1.anzChange(Sender: TObject);
begin

while anz.Lines.Count>100 do anz.Lines.Delete(0);

end;

end.


Server Log nach 2 Versuchen:
ausblenden Quelltext
1:
2:
3:
4:
OPEN 127.0.0.1
Read
Shoutcast restart
CLOSE 127.0.0.1


mfg. Killmag10

_________________
Mega-inkompetente Computer-ruinierende Organisation spioniert ohne funktionierende Technik