高手过来看看INDY中UDP通讯的问题 ( 积分: 100 )

  • 主题发起人 ounettiger
  • 开始时间
O

ounettiger

Unregistered / Unconfirmed
GUEST, unregistred user!
我用INDY10(版本:10.1.6)做个通讯程序,大家看一下源代码,帮我分析一下是什么问题,100分相送,分不够咱再加:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdUDPClient, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPServer, IdSocketHandle,IdGlobal, IdAntiFreezeBase, IdAntiFreeze;

type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
IdUDPClient1: TIdUDPClient;
Button1: TButton;
Edit1: TEdit;
IdAntiFreeze1: TIdAntiFreeze;
procedure Button1Click(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TIdBytes; ABinding:
TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
mmbyte:TIdBytes;
str:string;
begin
IdUDPClient1.Host:=Edit1.Text;
IdUDPClient1.ReceiveTimeout:=250;
IdUDPClient1.Active:=True;
IdUDPClient1.Connect;
if IdUDPClient1.Connected then IdUDPClient1.Send('READY_?');
IdUDPClient1.ReceiveBuffer(mmbyte);
BytesToRaw(mmbyte,str,Length(mmbyte));

ShowMessage(IntToStr(Length(mmbyte)));
//if IdUDPClient1.ReceiveString()='READY_OK' then ShowMessage('Connected');

end;

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TIdBytes;
ABinding: TIdSocketHandle);
var
sbyte:TIdBytes;
sstr:array[0..10] of char;
begin
sstr:='READY_OK';
sbyte:=RawToBytes(sstr,Length(sstr));

if string(adata)='READY_?' then
begin
ABinding.SendTo(ABinding.PeerIP,ABinding.PeerPort,AData);
end;
end;

end.
 
IdUDPClient1.ReceiveBuffer(mmbyte);
BytesToRaw(mmbyte,str,Length(mmbyte));

ShowMessage(IntToStr(Length(mmbyte)));
//if IdUDPClient1.ReceiveString()='READY_OK' then ShowMessage('Connected');

这些补应该写在Button1Click里
 
应该怎么写,能否请寒萧兄明示。同时请寒萧兄告知代码写在BUTTON1CLICK里边为什么不可以?
 
是不是ABinding.SendTo(ABinding.PeerIP,ABinding.PeerPort,AData);一句没有起作用啊?
我发现在UDPCLIENT的ONCONNECT事件中加入showmessage('YES');该测试语句没有反映,是否表示SERVER并没有与客端建立起连接,或者是客户与服务器端已经在连接状态?
请高手们帮忙解决啊,在线急等!
 
另外,只要将UDPSERVER的threadedevent设置为TRUE后,再用同样的代码连接就会出现问题,一方面说是PEERIP什么的已被使用,另一方面说是'socket error # 10040 message to long',是不是UDPSERVER有BUG啊.查看网上资料,说是使用上线程后UDPSERVER会自动处理的,我碰到的问题怎么这么多啊!
 
盼啊!
谁能解决我提的问题,给300分.
 
本问题我自己已经解决.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
601
import
I
S
回复
0
查看
823
SUNSTONE的Delphi笔记
S
顶部