大家给看看,使用nmudp为何我的serv端总是过一会就出错?另如何其捕获错误事件?(200分)

  • 主题发起人 主题发起人 chinasea
  • 开始时间 开始时间
C

chinasea

Unregistered / Unconfirmed
GUEST, unregistred user!
client端:
procedure TForm1.Timer1Timer(Sender: TObject);
var rndint:shortint;
buf:byte;
mystream:TMemorystream;
begin
Randomize;
rndint:=random(5);
nmudp1.RemoteHost:='192.168.1.5';
nmudp1.ReportLevel:=status_basic;
nmudp1.RemotePort:=6668;
mystream:=tmemorystream.create;
try
copymemory(@buf,@rndint,1);
mystream.write(buf,1);
nmudp1.sendstream(mystream);
finally
mystream.free;
end;
end;


server端

procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var num:array of byte;
inttmp:shortint;
mystream:Tmemorystream;
begin
mystream:=tmemorystream.create;
try
nmudp1.readstream(mystream);
setlength(num,numberbytes);
mystream.read(num,numberbytes);
copymemory(@inttmp,@num,1);
case inttmp of
1:
begin
shape1.Brush.Color:=clred;
shape2.Brush.Color:=clred;
end;
2: begin
shape1.Brush.Color:=clGreen;
shape2.Brush.Color:=clGreen;
end;
3: begin
shape1.Brush.Color:=clBlue;
shape2.Brush.Color:=clBlue;
end;

4:begin
shape1.Brush.Color:=clAqua;
shape2.Brush.Color:=clAqua;
end;
5:begin
shape1.Brush.Color:=clYellow;
shape2.Brush.Color:=clYellow;
end;
else
begin
shape1.Brush.Color:=clWhite;
shape2.Brush.Color:=clWhite;
end;
end;
finally
mystream.Free;
end;
end;
 
后退
顶部