F
felong11
Unregistered / Unconfirmed
GUEST, unregistred user!
我用nmudp做了一个网络广播的东西
一个服务端负责广播消息然后再建立稳定的连接(tcp)
我在发送接受的时候发现有时候可以接受有时候不能,
发送的时候也一样,很奇怪。
发送通过一个timer定时发送:
procedure TFormStatus.TimerTimer(Sender: TObject);
var
MyStream: TMemoryStream;
TmpStr: String;
begin
//定时广播广播帧
BOARDCASTFRAME:=POTOCALHEAD+'@'+FormMain.sa.ParallelControllerAttributes.ContrellerIP+
'@'+strpas(FormMain.sa.ParallelControllerAttributes.ControlPort)+
'@'+strpas(FormMain.sa.ParallelControllerAttributes.DataPort);
TmpStr := BOARDCASTFRAME;
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(TmpStr));
FormMain.UDPbc.SendStream(MyStream);
EditBCStatus.Lines.Add('{'+datetimetostr(now)+'} Sent>>"'+TmpStr+'"');
FormMain.ScrollToBottom;
finally
MyStream.Free;
end;
end;
接收:
procedure TFormMain.UDPbcDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
TmpStr: String;
BCField:TBCField;
begin
MyStream := TMemoryStream.Create;
try
UDPbc.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
FormStatus.EditBCStatus.Lines.Add('{'+datetimetostr(now)+'} < '+
FromIP+': '+TmpStr);
if (not HASSETCLIENTSOCKET) then
if (not cs.Active) then
begin
HASSETCLIENTSOCKET:=true;
BCField:=PhaseBCFrame(TmpStr);
cs.Address:=BCField.ServerIp;
cs.Port:=BCField.CtrlPort;
cs.Active:=true;
FtpClient.Port:=BCField.DataPort;
IdEcho1.Host:=BCField.ServerIp;
IdEcho1.Connect;
IdEcho1.Echo(CEchoFrame);
LOCALANDCONTROLLERECHOTIME:=IdEcho1.EchoTime;
cs.Socket.SendText(ComposeLocalInfoFrame);
FtpClient.Port:=BCField.DataPort;
FtpClient.UserName:=FormSettings.EditFTPUsername.Text;
FtpClient.PassWord:=FormSettings.GetFTPPsw;
FtpClient.Connect;
end;
finally
MyStream.Free;
end;
end;
都是经典的用例。
我就不懂了!
奇怪!!
请高手指正。
或者能告诉我其他那个udp的控件较好。谢谢
一个服务端负责广播消息然后再建立稳定的连接(tcp)
我在发送接受的时候发现有时候可以接受有时候不能,
发送的时候也一样,很奇怪。
发送通过一个timer定时发送:
procedure TFormStatus.TimerTimer(Sender: TObject);
var
MyStream: TMemoryStream;
TmpStr: String;
begin
//定时广播广播帧
BOARDCASTFRAME:=POTOCALHEAD+'@'+FormMain.sa.ParallelControllerAttributes.ContrellerIP+
'@'+strpas(FormMain.sa.ParallelControllerAttributes.ControlPort)+
'@'+strpas(FormMain.sa.ParallelControllerAttributes.DataPort);
TmpStr := BOARDCASTFRAME;
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(TmpStr));
FormMain.UDPbc.SendStream(MyStream);
EditBCStatus.Lines.Add('{'+datetimetostr(now)+'} Sent>>"'+TmpStr+'"');
FormMain.ScrollToBottom;
finally
MyStream.Free;
end;
end;
接收:
procedure TFormMain.UDPbcDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
TmpStr: String;
BCField:TBCField;
begin
MyStream := TMemoryStream.Create;
try
UDPbc.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
FormStatus.EditBCStatus.Lines.Add('{'+datetimetostr(now)+'} < '+
FromIP+': '+TmpStr);
if (not HASSETCLIENTSOCKET) then
if (not cs.Active) then
begin
HASSETCLIENTSOCKET:=true;
BCField:=PhaseBCFrame(TmpStr);
cs.Address:=BCField.ServerIp;
cs.Port:=BCField.CtrlPort;
cs.Active:=true;
FtpClient.Port:=BCField.DataPort;
IdEcho1.Host:=BCField.ServerIp;
IdEcho1.Connect;
IdEcho1.Echo(CEchoFrame);
LOCALANDCONTROLLERECHOTIME:=IdEcho1.EchoTime;
cs.Socket.SendText(ComposeLocalInfoFrame);
FtpClient.Port:=BCField.DataPort;
FtpClient.UserName:=FormSettings.EditFTPUsername.Text;
FtpClient.PassWord:=FormSettings.GetFTPPsw;
FtpClient.Connect;
end;
finally
MyStream.Free;
end;
end;
都是经典的用例。
我就不懂了!
奇怪!!
请高手指正。
或者能告诉我其他那个udp的控件较好。谢谢