请教Indy高手(63分)

  • 主题发起人 主题发起人 飞来石
  • 开始时间 开始时间

飞来石

Unregistered / Unconfirmed
GUEST, unregistred user!
我想使用indy控件实现2个用户之间的连接,可以聊天,传输文件.而且不用server,就2个client,能行吗? 谢谢了,全部分送上
 
用TIdUDP即可
 
能否详细一点呢?我从来没有用过indy呢
 
没有人会啊?我出rmb有人愿意帮忙么
 
RMB又何必呢?这些问题看看Indy的DEMO就可以解决了!
//以下代码来自Indy的DEMO,
//稍微对客户端和服务器端进行整合就可以达到两个客户端通讯的效果。
//客户端
unit UDPClientMain;

interface

uses
Windows, Messages, Graphics, Controls, Forms, Dialogs, IdWinsock2, stdctrls,
SysUtils, Classes, IdBaseComponent, IdAntiFreezeBase, IdAntiFreeze,
IdComponent, IdUDPBase, IdUDPClient, IdStack;

type
TUDPMainForm = class(TForm)
SourceGroupBox: TGroupBox;
HostNameLabel: TLabel;
HostAddressLabel: TLabel;
HostName: TLabel;
HostAddress: TLabel;
UDPAntiFreeze: TIdAntiFreeze;
PortLabel: TLabel;
Port: TLabel;
DestinationLabel: TLabel;
DestinationAddress: TLabel;
BufferSizeLabel: TLabel;
BufferSize: TLabel;
UDPMemo: TMemo;
SendButton: TButton;
UDPClient: TIdUDPClient;
procedure FormCreate(Sender: TObject);
procedure SendButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
UDPMainForm: TUDPMainForm;

implementation

const
HOSTNAMELENGTH = 80;
RECIEVETIMEOUT = 5000; // milliseconds

{$R *.DFM}

procedure TUDPMainForm.FormCreate(Sender: TObject);
begin
Randomize; // remove if you want reproducible results.
HostName.Caption := UDPClient.LocalName;
HostAddress.Caption := GStack.LocalAddress;
Port.Caption := IntToStr(UDPClient.Port);
DestinationAddress.Caption := UDPClient.Host;
BufferSize.Caption := IntToStr(UDPClient.BufferSize);
UDPClient.ReceiveTimeout := RECIEVETIMEOUT;
end;

procedure TUDPMainForm.SendButtonClick(Sender: TObject);
var
MessageID: Integer;
ThisMessage: String;
ReceivedString: String;
begin
MessageID := Random(MAXINT);
ThisMessage := 'Message: ' + IntToStr(MessageID);
UDPMemo.Lines.Add('Sending ' + ThisMessage);
UDPClient.Send(ThisMessage);
ReceivedString := UDPClient.ReceiveString();
if ReceivedString = '' then
UDPMemo.Lines.Add('No response received from the server after ' + IntToStr(UDPClient.ReceiveTimeout) + ' millseconds.')
else
UDPMemo.Lines.Add('Received: ' + ReceivedString)
end;

end.

//服务器端
unit UDPServerMain;

interface

uses
Windows, Messages, Graphics, Controls, Forms, Dialogs, IdWinsock2, stdctrls,
SysUtils, Classes, IdBaseComponent, IdAntiFreezeBase, IdAntiFreeze,
IdComponent, IdUDPBase, IdUDPClient, IdStack, IdUDPServer, IdSocketHandle;


type
TUDPMainForm = class(TForm)
SourceGroupBox: TGroupBox;
HostNameLabel: TLabel;
HostAddressLabel: TLabel;
HostName: TLabel;
HostAddress: TLabel;
UDPServer: TIdUDPServer;
UDPAntiFreeze: TIdAntiFreeze;
PortLabel: TLabel;
Port: TLabel;
BufferSizeLabel: TLabel;
BufferSize: TLabel;
UDPMemo: TMemo;
procedure FormCreate(Sender: TObject);
procedure UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;

var
UDPMainForm: TUDPMainForm;

implementation

const
HOSTNAMELENGTH = 80;

{$R *.DFM}

procedure TUDPMainForm.FormCreate(Sender: TObject);
begin
HostName.Caption := UDPServer.LocalName;
HostAddress.Caption := GStack.LocalAddress;
Port.Caption := IntToStr(UDPServer.DefaultPort);
BufferSize.Caption := IntToStr(UDPServer.BufferSize);
UDPServer.Active := True;
end;

procedure TUDPMainForm.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
s: String;
begin
DataStringStream := TStringStream.Create('');
try
DataStringStream.CopyFrom(AData, AData.Size);
UDPMemo.Lines.Add('Received "' + DataStringStream.DataString + '" from ' + ABinding.PeerIP + ' on port ' + IntToStr(ABinding.PeerPort));
s := 'Replied from ' + UDPServer.LocalName + ' to "' + DataStringStream.DataString + '"';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, s[1], Length(s));
finally
DataStringStream.Free;
end;
end;


end.
 
我也正要开发这方面的功能,关注中。。。
 
当然行了,我这有INDY的所有DEMO,要的话留下EMAIL
 
先谢谢无头骑士, 给我一份wyqxp@163.com[:D]
 
谢谢,来一份!
yitangliang@hotmail.com
 
几年前写的,可以看看,需要源码,与我联系。
http://www.ctms.com.cn/soft/NetPower.rar
 
给我一份代码吧,我学习学习,谢谢了
pdscwf@sina.com
 
也给我一份吧
meilixueshan2008@163.com
 
后退
顶部