Top.Mail.Ru
Мастера DELPHI, Delphi programming community Титульная страница Поиск, карта сайта Написать письмо 
| Новости | Поиск | FAQ | Статьи | Книги | Новости VCL |
| Форумы ЧАТ | Орешник | KOL и MCK |
Форумы -» Основная («Начинающим»)/ Базы / WinAPI / Компоненты / Сети / Media / Игры / Corba и COM / KOL / FreePascal / .Net / Прочее / RSDN.ORG

« предыдущая ветвь | форум | следующая ветвь »

idTCPServer и idTCPClient как обмениваться сообщениями ? [D7, Win2k]


banderas ©   (2004-07-03 19:33:00) 

Как с помощью idTCPServer и idTCPClient передовать сообщения


alienserg   (2004-07-06 05:16:00) [1]

пример есть в IndyDemos\Chat


banderas   (2004-07-05 13:19:00) [2]

ОК ШПАСИБО!


Winni   (2004-07-04 22:15:00) [3]

{***************************************************************
*
* Project  : CBClient
* Unit Name: MainForm
* Purpose  :
* Version  : 1.0
* Date  : Wed 25 Apr 2001  -  01:12:33
* Author  :
* History  :
* Tested  : Wed 25 Apr 2001  // Allen O'Neill <allen_oneill@hotmail.com>
*
****************************************************************}

unit MainForm;

interface

uses
{$IFDEF Linux}
  QGraphics,  QControls,  QForms,  QDialogs,  QComCtrls,  QMenus,  QButtons,
    QExtCtrls,  QStdCtrls,
{$ELSE}
  Graphics,  Controls,  Forms,  Dialogs,  ComCtrls,  Menus,  Buttons,  ExtCtrls,
    StdCtrls,
{$ENDIF}
 windows, messages,  ToolWin, spin,  SysUtils,  Classes, IdBaseComponent,
   IdComponent, IdTCPConnection, IdTCPClient;

type
 TForm1 = class(TForm)
   Label1: TLabel;
   edUserName: TEdit;
   Label2: TLabel;
   edServer: TEdit;
   Label3: TLabel;
   lbClients: TListBox;
   Label4: TLabel;
   memLines: TMemo;
   Label5: TLabel;
   edMessage: TEdit;
   SpeedButton1: TSpeedButton;
   IdTCPClient1: TIdTCPClient;
   Timer1: TTimer;
   Label6: TLabel;
   sePort: TSpinEdit;
   Button1: TButton;
   procedure FormShow(Sender: TObject);
   procedure Timer1Timer(Sender: TObject);
   procedure SpeedButton1Click(Sender: TObject);
   procedure IdTCPClient1Connected(Sender: TObject);
   procedure edMessageKeyPress(Sender: TObject; var Key: Char);
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$IFDEF MSWINDOWS}{$R *.dfm}{$ELSE}{$R *.xfm}{$ENDIF}

procedure TForm1.FormShow(Sender: TObject);
begin
 width := width + 1;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
 Com,
   Msg: string;
begin
 if not IdTcpClient1.Connected then
   exit;

 Msg := IdTCPClient1.ReadLn('', 5);

 if Msg <> '' then
   if Msg[1] <> '@' then
   begin
     { Not a system command }
     memLines.Lines.Add(Msg);
   end
   else
   begin
     { System command }
     Com := UpperCase(Trim(Copy(Msg, 2, Pos(':', Msg) - 2)));
     Msg := UpperCase(Trim(Copy(Msg, Pos(':', Msg) + 1, Length(Msg))));
     if Com = 'CLIENTS' then
       lbClients.Items.CommaText := Msg;
   end;

end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
 if (edUserName.Text <> '') and
   (edServer.Text <> '') and
   SpeedButton1.Down then
 begin
   IdTCPClient1.Host := edServer.Text;
   IdTCPClient1.Port := sePort.Value;
   if SpeedButton1.Down then
     IdTCPClient1.Connect;
 end
 else
 begin
   if (edUserName.Text = '') or
     (edServer.Text = '') then
     ShowMessage('You must put in a User Name and server name to connect.');
   SpeedButton1.Down := false;
 end;
end;

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

procedure TForm1.edMessageKeyPress(Sender: TObject; var Key: Char);
begin
 if key = #13 then
 begin
   IdTCPClient1.WriteLn(edMessage.Text);
   edMessage.Text := '';
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 IdTCPClient1.WriteLn('@' + 'CLIENTS:REQUEST');
end;

end.


banderas   (2004-07-04 14:42:00) [4]

Нее там используется не idTCPServer и idTCPClient а просто  TCPServer и TCPClient которые не являются компонентами indy


Winni   (2004-07-04 13:18:00) [5]

Пример есть в комплекте Delphi6 (и Delphi7): Delphi6\Demos\Internet\Chat здесь есть папки Client и Server. Все работает.


Наверх

Титульная страница Поиск, карта сайта Написать письмо