Einzelnen Beitrag anzeigen

Benutzerbild von ghost007
ghost007

Registriert seit: 31. Okt 2005
Ort: München
1.024 Beiträge
 
Delphi 7 Personal
 
#3

Re: Problem mit Server/Client funktioniert NUR lokal ?!

  Alt 13. Jan 2006, 21:04
hier der quelltext für meinen Client:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdIPWatch, ExtCtrls,
  ScktComp, XPMan;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Memo1: TMemo;
    Label4: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ClientSocket1: TClientSocket;
    Timer1: TTimer;
    IdIPWatch1: TIdIPWatch;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Delay(time: word);
    procedure Button3Click(Sender: TObject);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Nickname: String;
  Nachricht:string;

implementation

{$R *.dfm}

procedure TForm1.Delay(time:word);
var Start: Integer;
begin
  Start:= GetTickCount;
  while (GetTickCount)-(Start) <= time do
     Application.ProcessMessages;
end;    

procedure TForm1.FormCreate(Sender: TObject);
begin
Label3.Caption:=IdIPWatch1.LocalIP;
Button3.Enabled:=false;
Button2.Enabled:=false;
Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Nickname:=Edit3.Text;
Button1.Enabled:=false;
Edit3.Enabled:=false;
Button2.Enabled:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ClientSocket1.Host:=Edit2.Text;
ClientSocket1.Port:=7530;
ClientSocket1.Active:=true;
Button2.Enabled:=false;
Edit2.Enabled:=false;
Button3.Enabled:=true;
Nachricht:=Edit3.Text+' hat den Raum betreten';
delay(500);
ClientSocket1.Socket.SendText(Nachricht);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if (ClientSocket1.Active=true) and (Edit1.Text<>'') then
ClientSocket1.Socket.SendText(Nickname+': '+Edit1.Text);
Edit1.Text:='';
Memo1.Perform(WM_VSCROLL,SB_BOTTOM,0);
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
begin
Form1.Show;
Memo1.Lines.Add(Socket.ReceiveText);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label3.Caption:=IdIPWatch1.LocalIP;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ClientSocket1.Socket.SendText(Edit3.Text +' hat den Chat verlassen.');
delay(500);
ClientSocket1.Close;
end;

end.
Hier der quelltext für meinen Server:
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, StdCtrls, IdBaseComponent, IdComponent, IdIPWatch;

type
  TForm1 = class(TForm)
    IdIPWatch1: TIdIPWatch;
    Memo1: TMemo;
    ServerSocket1: TServerSocket;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  i:integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Label2.Caption:=IDIPWatch1.LocalIP;
ServerSocket1.Port:=7530;
ServerSocket1.Active:=true;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
Text:=Socket.ReceiveText;
Memo1.Lines.Add(TimeToStr(now)+': '+Text);
    For I:=0 to ServerSocket1.Socket.ActiveConnections - 1 do
     begin
     with ServerSocket1.Socket.Connections [I] do
    if (Connected) then SendText(Text);
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ServerSocket1.Close;
Memo1.Lines.SaveToFile('C:/Chat-Log.txt');
end;

end.
Ich hoffe das es jetzt einfacher ist mir zu helfen
Christian
  Mit Zitat antworten Zitat