求:使用IdTcpClient和IdTcpServer相互发送数据(全部家当)(100分)

  • 主题发起人 主题发起人 flymyhart
  • 开始时间 开始时间
F

flymyhart

Unregistered / Unconfirmed
GUEST, unregistred user!
多个 client 和一个 server 连接

server 可以选择向某一个 client 发送消息

client 能显示接收的消息 ....
 
写个例子来看看呢?

要用 idTCPServer IdTCPClient
 
======================server============================
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;

type
PSocketThread=^TSocketThread;
TSocketThread=Record
SocketThread:TIdPeerThread;
Next:PSocketThread;
end;

var
Form1: TForm1;
ST_Head,ST_End:PSocketThread;
ST_Count:integer;

implementation

{$R *.dfm}

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var
PST_:PSocketThread;
begin
New(PST_);
PST_^.SocketThread:=AThread;
PST_^.Next:=nil;
if ST_Count=0 then
begin
ST_Head:=PST_;
ST_End:=ST_Head;
end
else
begin
ST_End^.Next:=PST_;
ST_End:=PST_;
end;
ST_Count:=ST_Count+1;
Edit1.Text:=IntToStr(ST_Count);
end;

procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
var
PST_,PST_0:PSocketThread;
begin
PST_:=ST_Head;
PST_0:=ST_Head;
while PST_<>nil do
begin
if PST_^.SocketThread.ThreadID=AThread.ThreadID then
begin
PST_0^.Next:=PST_^.Next;
Dispose(PST_);
ST_Count:=ST_Count-1;
Edit1.Text:=IntToStr(ST_Count);
end else
begin
PST_0:=PST_;
PST_:=PST_^.Next;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
PST_:PSocketThread;
begin
PST_:=ST_Head;
while PST_<>nil do
begin
PST_^.SocketThread.Connection.WriteLn('To U '+IntToStr(PST_^.SocketThread.ThreadID)+#$A);
PST_:=PST_^.Next;
end;
end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
Memo1.Lines.Add(AThread.Connection.ReadLn);
end;

end.




================================client====================================
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient;

type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure IdTCPClient1Connected(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure IdTCPClient1Disconnected(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
td:Dword;
doRead:boolean;

implementation

{$R *.dfm}

procedure ReadThread;
var
s:String;
begin
form1.Memo1.Lines.Add('Begin reading...');
s:=Form1.IdTCPClient1.ReadLn;
while doRead do
begin
s:=Form1.IdTCPClient1.ReadLn;
form1.Memo1.Lines.Add(s);
sleep(100);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPClient1.Connect(3000);
doRead:=true;
CreateThread(nil,0,@ReadThread,nil,0,td);
end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('connected to server');
end;

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

procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
ExitThread(td);
Memo1.Lines.Add('disConnected from server');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
doRead:=false;
IdTCPClient1.Disconnect;
ExitThread(td);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
// IdTCPClient1.Disconnect;
doRead:=false;
end;

end.
 
这里有例子http://61.152.210.98:8080/stuff/chat.rar
 
多人接受答案了。
 

Similar threads

后退
顶部