[?]TNMUDP控件接收数据时为什么会有乱码??(其它简单的代码省去了)代码如下:
点发送按钮:
procedure TForm1.Button1Click(Sender: TObject);
var txt
char;
begin
if memo1.Text ='' then
begin
statusbar1.SimpleText :='null to send!';
exit;
end;
GetMem(txt,length(memo1.Text)+1);
Zeromemory(txt,length(memo1.Text )+1);
txt:=pchar(memo1.Text );
nmudp1.SendBuffer(txt^,length(memo1.Text ));
end;
接收时:
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var txt
char;
count:integer;
begin
statusbar1.SimpleText :='receving data';
getmem(txt,numberbytes+1);
zeromemory(txt,numberbytes+1);
nmudp1.ReadBuffer(txt^,count);
memo2.Lines.Add('À´×Ô:'+FromIP+'¶Ë¿Ú:'+inttostr(port)+'ÄÚÈÝΪ:'+txt);
freemem(txt);
statusbar1.SimpleText :='data received';
end;
当字符大于5或6个时,后面的数据就成了乱码,IT同胞们指点.
getmem(txt,numberbytes+1);接收缓冲区定义大小应该没有问题吧??????