unit Sockets;

interface

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

type
  THauptformular = class(TForm)
    ClientSocket: TClientSocket;
    ServerSocket: TServerSocket;
    Server: TButton;
    Client: TButton;
    ServerDicht: TButton;
    ClientDicht: TButton;
    ClientText: TEdit;
    ServerText: TEdit;
    Senden: TButton;
    procedure ServerClick(Sender: TObject);
    procedure ClientClick(Sender: TObject);
    procedure ServerDichtClick(Sender: TObject);
    procedure ClientDichtClick(Sender: TObject);
    procedure SendenClick(Sender: TObject);
    procedure ServerSocketClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocketClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocketClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocketConnect(Sender: TObject;
      Socket: TCustomWinSocket);
  private
    { Private-Deklarationen}
  public
    { Public-Deklarationen}
  end;

var
  Hauptformular: THauptformular;

implementation

{$R *.DFM}

Function Quersumme(X:integer):Integer;
begin
  if X <= 9 then
    Quersumme := X
  else
    Quersumme := Quersumme(X div 10) + X mod 10
end;

procedure THauptformular.ServerClick(Sender: TObject);
begin
  Serversocket.Active := true;
  Hauptformular.Caption := 'Server';
  Server.Enabled := false;
  Client.Enabled := false;
  ServerDicht.Enabled := true;
end;

procedure THauptformular.ClientClick(Sender: TObject);
begin
  Clientsocket.Active := true;
end;

procedure THauptformular.ServerDichtClick(Sender: TObject);
begin
  Close;
end;

procedure THauptformular.ClientDichtClick(Sender: TObject);
begin
  Close
end;

procedure THauptformular.SendenClick(Sender: TObject);
begin
  ClientSocket.Socket.Sendtext(ClientText.Text)
end;

procedure THauptformular.ServerSocketClientRead(Sender: TObject;
  Socket: TCustomWinSocket);

var I,P: Integer; Q,Z: String;

begin
  Q := Socket.ReceiveText;
  ShowMessage('SERVER - Auftrag erhalten');
  Val(Q,I,P);
  if (P=0) and (I>0) then begin
    Str(Quersumme(I),Z)
  end else
    Z := 'Zahlenfehler';
  ServerText.Text := Q + ' => ' + Z;
  Socket.SendText(Z);
end;

procedure THauptformular.ClientSocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ServerText.Text := Socket.ReceiveText;
end;

procedure THauptformular.ServerSocketClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ShowMessage('SERVER - Client angeschlossen');
end;

procedure THauptformular.ServerSocketClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ShowMessage('SERVER - Client hat mich verlassen');
end;

procedure THauptformular.ClientSocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Hauptformular.Caption := 'Client';
  Client.enabled := false;
  Server.enabled := false;
  Clientdicht.enabled := true;
  Senden.enabled := true;
end;

end.