Einzelnen Beitrag anzeigen

bl4ckb1rd

Registriert seit: 3. Okt 2008
53 Beiträge
 
#13

Re: Finde Fehler bei meinem Chat Programm nicht

  Alt 18. Jan 2009, 14:37
Also mein 1ter Schritt ist es die SErver-Kommunikation hinzubekommen.

Hier der aktuelle Code vom Client:

Delphi-Quellcode:
unit Unit1;

interface

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


type TChatNachricht = Record
        nick: string[255];
        chat: string[255];
        state: string[255];
        hand: string[255];
        table: string[255];

      end;


type
  TForm1 = class(TForm)
    Label1: TLabel;
    connect: TButton;
    ip: TEdit;
    Label2: TLabel;
    port: TEdit;
    Label3: TLabel;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    GroupBox3: TGroupBox;
    Edit3: TEdit;
    GroupBox4: TGroupBox;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    Image5: TImage;
    Image6: TImage;
    Image7: TImage;
    Button7: TButton;
    XPManifest1: TXPManifest;
    Memo1: TMemo;
    chat: TEdit;
    send: TButton;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    nick: TEdit;
    ClientSocket1: TClientSocket;
    procedure sendClick(Sender: TObject);
    procedure connectClick(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure ClientSocket1Read (Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientSocket1Connect (Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button7Click(Sender: TObject);
begin
close;
end;

//---------------------------------------------------------------------------------------------------------

//*********************************** Client READ **********************************************************

procedure TForm1.ClientSocket1Read (Sender: TObject; Socket: TCustomWinSocket);
var Nachricht: TChatNachricht;
begin
  Socket.ReceiveBuf (Nachricht, SizeOf(Nachricht));

  if Nachricht.state = 'msgthen begin
    Memo1.lines.add (Nachricht.nick+': '+Nachricht.chat);
  end
  else
  if Nachricht.state <> 'msgthen begin
    Memo1.lines.add ('Es wurden unbekannte dateien empfangen');
  end;

end;



procedure TForm1.ClientSocket1Connect (Sender: TObject; Socket: TCustomWinSocket);
begin
  Memo1.Lines.Add ('Status: Verbindung wurde erfolgreich hergestellt');
end;




//---------------------------------------------------------------------------------------------------------


procedure TForm1.connectClick(Sender: TObject);
begin


  if clientsocket1.active = false then begin

    clientsocket1.address := ip.text;
    clientsocket1.port := strtoint(port.text);
    clientsocket1.open;

    ip.enabled := false;
    port.enabled := false;
    nick.enabled := false;
    connect.Caption := 'Disconnect';
  end
  else
  if clientsocket1.active then begin

    clientsocket1.Close;

    ip.enabled := true;
    port.enabled := true;
    nick.enabled := true;
    connect.Caption := 'Connect';
  end;



end;



//------------- Client Send -----------------------------------------------------------------

procedure TForm1.sendClick(Sender: TObject);
var
Nachricht: TChatNachricht;
begin

  if clientsocket1.active then begin

    Nachricht.chat := chat.Text;
    Nachricht.nick := nick.text;
    Nachricht.state := 'chat';
    Nachricht.hand := '0';
    Nachricht.table := '0';

    ClientSocket1.Socket.SendBuf (Nachricht, SizeOf(Nachricht));


    chat.text := '';

    exit;
  end;

  Memo1.lines.add ('Please connect to the server before chatting!');

  chat.Text := '';



end;

end.
Und hier der Code vom Server:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, XPMan, ScktComp;


type TChatNachricht = Record
        nick: string[255];
        chat: string[255];
        state: string[255];
        hand: string[255];
        table: string[255]

      end;



type
  TForm1 = class(TForm)
    Label1: TLabel;
    Memo1: TMemo;
    chat: TEdit;
    send: TButton;
    XPManifest1: TXPManifest;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Edit2: TEdit;
    kick: TButton;
    GroupBox2: TGroupBox;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    GroupBox3: TGroupBox;
    Edit3: TEdit;
    Label26: TLabel;
    Label27: TLabel;
    port: TEdit;
    startserver: TButton;
    kickall: TButton;
    exit: TButton;
    Label28: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    ServerSocket1: TServerSocket;
    procedure sendClick(Sender: TObject);
    procedure startserverClick(Sender: TObject);
    procedure exitClick(Sender: TObject);
    procedure ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientRead (Sender: TObject; Socket: TCustomWinSocket);

  private
    { Private-Deklarationen }

  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


{*******************  ON CLIEN CONNET**************************************   ****************************}

procedure TForm1.ServerSocket1ClientConnect (Sender: TObject; Socket: TCustomWinSocket);
var Nachricht: TChatNachricht;
    var I: Integer;
begin
  Memo1.Lines.Add ('Status: Neuer user verbunden');

  Nachricht.nick := 'Server';
  Nachricht.state :='msg';
  Nachricht.chat := 'Willkommen am Chat-Server';
  Socket.SendBuf (Nachricht, SizeOf(Nachricht));

  with ServerSocket1.Socket do begin
    Nachricht.nick := 'Server';
    Nachricht.state :='msg';
    Nachricht.chat := 'Jetzt ' + IntToStr(ActiveConnections) + ' Teilnehmer';
    for I := 0 to ActiveConnections-2 do
      Connections[I].SendBuf (Nachricht, SizeOf(Nachricht))
  end; {with}

  Memo1.Lines.add ('New user connected');

end;




{*******************  ************************************** ****************************}



procedure TForm1.exitClick(Sender: TObject);
begin
serversocket1.close;
close;
end;

//------------- SERVER ON READ -----------------------------------------------------------------
procedure TForm1.ServerSocket1ClientRead (Sender: TObject; Socket: TCustomWinSocket);
var
Nachricht : TChatNachricht;
begin

  socket.receiveBuf(Nachricht, SizeOf (Nachricht));

  if Nachricht.state = 'msgthen begin
    Memo1.Lines.add (Nachricht.nick+ ': '+ Nachricht.chat);
  end;

  if Nachricht.state <> 'msgthen begin
    Memo1.Lines.add ('Error');
  end;





end;




procedure TForm1.startserverClick(Sender: TObject);
begin

  if serversocket1.active = false then begin
    serversocket1.Port := strtoint (port.text);
    port.enabled := false;
    startserver.caption := 'Stop Server';
    serversocket1.open;
  end
  else
  if serversocket1.active = true then begin
    serversocket1.close;
    port.Enabled := true;
    startserver.caption := 'Start Server';
  end;



end;


//------------- CHAT SENDEN -----------------------------------------------------------------

procedure TForm1.sendClick(Sender: TObject);
var
  Nachricht : TChatNachricht;
  i: Integer;
begin

  if serversocket1.Active then begin
    Nachricht.chat := chat.text;
    chat.text := '';

    Nachricht.state := 'msg';

    Nachricht.nick := 'Server';

      with ServerSocket1.Socket do begin
        for I := 0 to ActiveConnections-1 do
          Connections[I].SendBuf (Nachricht, SizeOf(Nachricht))
      end; {with}

    Memo1.Lines.add ('Server: '+ Nachricht.chat);
  end
  else begin
    Memo1.lines.add ('Error: Server not online!');
    chat.text := '';
  end;



end;

end.

Kann es sein das die sockets aufhören zu arbeiten wenn die Oberfläche zu viele inhalte enthält??
  Mit Zitat antworten Zitat