300分求BDS2006 indy10控件中IdTCPConnection, IdTCPClient的示例代码(100分)

  • 主题发起人 主题发起人 ww990
  • 开始时间 开始时间
W

ww990

Unregistered / Unconfirmed
GUEST, unregistred user!
1、示例代码应能在服务端显示上线客户端的IP地址、机器名、上线时间,是否在线等;
2、服务端停止时客户端应能检测到;
3、服务端可显示出上线客户端的数量;
4、服务端向客户端发送字符串,客户端收到后可显示出来;
5、客户端向服务端发送字符串,服务端收到后可显示出来。
另外200分,待回答后给全。
 
D6中有类似代码
 
bds2006中indy10的用法以及函数和D6中不一样,根本不能编译。
 
indy的官方网站有例子。不过1、2、3功能的实现不是分能解决的哈哈。因为在indy的newsgroup上面有很多类似的问题,都没有很好的解答。我认识的朋友有专门搞这个的,他给我们公司做项目就用indy,你提到的所有问题基本他都能解决了,很不好解决做了很多试验才用明白的,现在用在石油公司很稳定。如果你是公司需要,我可以问问这边做的人。别向我拍砖,没有免费的午餐啊。况且是人家饭碗啊,^_^。有意我就问问他,我也问问他看看能给点提示也行。
 
D7下以上这些都不是问题,例子很多,D10下实在不行的话,就只能改用D7了。个人学习而已,谈不上公司。
 
D10和D7应该没有什么差别啊。其实就是indy版本的问题,很容易如果有delphi7下面,只需在其基础上面改写读写操作的代码就可以了。也就是d10下indy10了,indy10主要通过iohandler进行读写处理。别的就没有很明显得差别了。
 
拿分来!

服务器:
unit IndyTCPServer;

interface

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

type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Edit1: TEdit;
Button3: TButton;
IdTCPServer1: TIdTCPServer;
Button4: TButton;
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure IdTCPServer1Disconnect(AContext: TIdContext);
procedure IdTCPServer1BeforeConnect(AContext: TIdContext);
procedure IdTCPServer1Connect(AContext: TIdContext);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses WinSock;

{$R *.dfm}

function IPAddrToName(IPAddr : String): String;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
WSAStartup($101, WSAData);
SockAddrIn.sin_addr.s_addr:= inet_addr(PChar(IPAddr));
HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt<>nil then
begin
result:=StrPas(Hostent^.h_name)
end
else
begin
result:='';
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
I : Integer;
Context: TIdContext;
begin
if not IdTCPServer1.Active then
Button1.Caption := '停止服务(&S)'
else begin
Button1.Caption := '启动服务(&S)';
for I := ListView1.Items.Count - 1 downto 0 do
begin
Context:= TIdContext(ListView1.Items.SubItems.Objects[0]);
if Context <> nil then
begin
ListView1.Items.Delete;
Context.Connection.Socket.Close;
end;
end;
ListView1.Tag := 0;
end;
IdTCPServer1.Active := Not IdTCPServer1.Active;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Context: TIdContext;
begin
if ListView1.ItemIndex < 0 then
begin
ShowMessage('请选择一个发送对象!');
Exit;
end;
Context := TIdContext(ListView1.Selected.SubItems.Objects[0]);
if Context <> nil then
Context.Connection.Socket.Close;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
Context: TIdContext;
begin
if Trim(Edit1.Text) = '' then Exit;
if ListView1.ItemIndex < 0 then
begin
ShowMessage('请选择一个发送对象!');
Exit;
end;
Context := TIdContext(ListView1.Selected.SubItems.Objects[0]);
if Context <> nil then
Context.Connection.Socket.WriteLn(Edit1.Text);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
I : Integer;
Context: TIdContext;
begin
if Trim(Edit1.Text) = '' then Exit;
for I := 0 to ListView1.Items.Count - 1 do
begin
Context := TIdContext(ListView1.Items.SubItems.Objects[0]);
if Context <> nil then
Context.Connection.Socket.WriteLn(Edit1.Text);
end;
end;

procedure TForm1.IdTCPServer1BeforeConnect(AContext: TIdContext);
begin
Memo1.Lines.Add(Format('用户请求连接:[IP:%s, Port: %d]', [AContext.Binding.PeerIP, AContext.Binding.PeerPort]));
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
Memo1.Lines.Add(Format('用户已连接:[IP:%s, Port: %d]', [AContext.Binding.PeerIP, AContext.Binding.PeerPort]));
with ListView1.Items.Add do
begin
Caption := IntToStr(ListView1.Tag);
SubItems.AddObject(AContext.Binding.PeerIP, AContext);
SubItems.Add(IntToStr(AContext.Binding.PeerPort));
SubItems.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss', now));
//不推荐增加主机名 要么从客户端发往服务器 我就先这么写了
SubItems.Add(IPAddrToName(AContext.Binding.PeerIP));
ListView1.Tag := ListView1.Tag + 1;
end;
end;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
var
I : Integer;
Context: TIdContext;
begin
Memo1.Lines.Add(Format('用户断开连接:[IP:%s, Port: %d]', [AContext.Binding.PeerIP, AContext.Binding.PeerPort]));
for I := ListView1.Items.Count - 1 downto 0 do
begin
Context:= TIdContext(ListView1.Items.SubItems.Objects[0]);
if Context = AContext then
begin
ListView1.Items.Delete;
Break;
end;
end;
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
S : string;
begin
S := AContext.Connection.Socket.ReadLn;
if S <> '' then
Memo1.Lines.Add(Format('收到来自[%s:%d]的消息: %s', [AContext.Binding.PeerIP, AContext.Binding.PeerPort, S]));
end;

end.




客户端:
unit TCPClient;

interface

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

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

var
Form2: TForm2;

procedure ReadSrvMsg;

implementation

const
RSMTimer: LongWord = 300;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
if not IdTCPClient1.Connected then
IdTCPClient1.Connect
else
IdTCPClient1.Disconnect;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
if not IdTCPClient1.Connected then Exit;
if Trim(Edit1.Text) = '' then Exit;
IdTCPClient1.Socket.WriteLn(Edit1.Text);
end;

procedure TForm2.IdTCPClient1Connected(Sender: TObject);
begin
Memo1.Lines.Add('连接成功! ' + IdTCPClient1.Host);
SetTimer(Handle, RSMTimer, 20, @ReadSrvMsg);
Button1.Caption := '断开连接(&D)';
end;

procedure TForm2.IdTCPClient1Disconnected(Sender: TObject);
begin
Memo1.Lines.Add('已断开连接!');
KillTimer(Handle, RSMTimer);
Button1.Caption := '连接(&C)';
end;

procedure ReadSrvMsg;
var
S: string;
begin
S := Form2.IdTCPClient1.Socket.ReadLn('', 10);
if S <> '' then
Form2.Memo1.Lines.Add('收到服务器消息: ' + S);
end;

end.
 
要不想复制就加msn:e-tkm@Hotmail.com 传给你.

另外 正归做C/S程序的一般都用Socket 不推荐用Indy!
 
你把代码发到我的邮箱ww990@126.com,收到立即给分。
 
接受答案了.
 
Miros到这里继续接分http://www.delphibbs.com/delphibbs/dispq.asp?lid=3532158
 
可否给个给我?
 
后退
顶部