Einzelnen Beitrag anzeigen

Benutzerbild von jfheins
jfheins

Registriert seit: 10. Jun 2004
Ort: Garching (TUM)
4.579 Beiträge
 
#8

Re: Client und Server in einem Prog - Code geht nicht

  Alt 12. Jun 2004, 22:51
hier der korekte Sourcecode:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdThreadMgr, IdThreadMgrDefault, IdTCPServer, IdBaseComponent, ComCtrls,
  IdComponent, IdSocketHandle, IdTCPConnection, IdTCPClient, StdCtrls, WinSock,
  IdAntiFreezeBase, IdAntiFreeze;

type
  TForm1 = class(TForm)
    IdTCPClient1: TIdTCPClient;
    IdTCPServer1: TIdTCPServer;
    IdThreadMgrDefault1: TIdThreadMgrDefault;
    Button1: TButton;
    ComboBox1: TComboBox;
    IdAntiFreeze1: TIdAntiFreeze;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure IdTCPServer1Execute(AThread: TIdPeerThread);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  mess1, mess2: string;
  Binding : TIdSocketHandle;
 // CommBlock, NewCommBlock: TCommBlock;

implementation

{$R *.dfm}

function GetIp: string;
var phoste: PHostEnt;
  Buffer: array [0..100] of char;
  WSAData: TWSADATA;
begin
  result:='';
  if WSAStartup($0101, WSAData) <> 0 then exit;
  GetHostName(Buffer,Sizeof(Buffer));
  phoste:=GetHostByName(buffer);
  if phoste = nil then result:='127.0.0.1'
  else result:=StrPas(inet_ntoa(PInAddr(phoste^.h_addr_list^)^));
  WSACleanup;
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
IdTCPServer1.Active := FALSE;

IdTCPServer1.Bindings.Clear;
Binding := IdTCPServer1.Bindings.Add;
Binding.IP := GetIP;
Binding.Port := 2222;

IdTCPServer1.Active := TRUE;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPServer1.Active := FALSE;

if (Combobox1.Text <> 'Bitte IP eingeben') then
begin
if (Combobox1.Text <> '') then
begin
IdTCPClient1.Host := Combobox1.Text;
IdTCPClient1.Port := 2222;
IdTCPClient1.Connect;
end;
end;

IdTCPServer1.Active := TRUE;
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
IdTCPClient1.WriteLn(Edit1.Text);
IdTCPClient1.Disconnect;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdTCPServer1.Active := FALSE;
IdTCPServer1.Destroy;
IdTCPClient1.Destroy;
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
showmessage (AThread.Connection.ReadLn());
end;

end.
  Mit Zitat antworten Zitat