J
jinmen
Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi编的的一个网络的程序,其中两个程序通过udp数据报的形式互相传送数据,但
是当传送的数据大于4个字符的时候就会出现乱码,不只是何缘故?
我的油箱地址是jinmen@263.net.
主要的源代码如下
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;//数据收到触发
NumberBytes: Integer; FromIP: String; Port: Integer);
var
TXT: PChar;
size:integer;
begin
StatusBar1.SimpleText:=''Receiving data'';
size:=NUmberBytes+1;
GetMem(txt,size);
FillChar(txt^,size,#0);
NMUDP1.ReadBuffer(TXT^,NumberBytes);
Memo1.Lines.Add(TXT);
FreeMem(Txt);
StatusBar1.SimpleText:=''data Received'';
end;
procedure TForm1.btnSendClick(Sender: TObject);//发送数据代码
var
txtchar;
size:integer;
begin
if Memo1.Text='''' then
begin
StatusBar1.SimpleText:=''Nothing to send'';
exit;
end;
size:=Length(Memo1.Text)+1;
GetMem(txt,size);
FillChar(txt^,size,#0);
StrPCopy(Txt,Memo1.Text);
NMUDP1.RemoteHost := Edit1.Text;
NMUDP1.RemotePort := StrToInt(Edit2.Text);
NMUDP1.SendBuffer(txt^,Length(Memo1.Text));
end;
是当传送的数据大于4个字符的时候就会出现乱码,不只是何缘故?
我的油箱地址是jinmen@263.net.
主要的源代码如下
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;//数据收到触发
NumberBytes: Integer; FromIP: String; Port: Integer);
var
TXT: PChar;
size:integer;
begin
StatusBar1.SimpleText:=''Receiving data'';
size:=NUmberBytes+1;
GetMem(txt,size);
FillChar(txt^,size,#0);
NMUDP1.ReadBuffer(TXT^,NumberBytes);
Memo1.Lines.Add(TXT);
FreeMem(Txt);
StatusBar1.SimpleText:=''data Received'';
end;
procedure TForm1.btnSendClick(Sender: TObject);//发送数据代码
var
txtchar;
size:integer;
begin
if Memo1.Text='''' then
begin
StatusBar1.SimpleText:=''Nothing to send'';
exit;
end;
size:=Length(Memo1.Text)+1;
GetMem(txt,size);
FillChar(txt^,size,#0);
StrPCopy(Txt,Memo1.Text);
NMUDP1.RemoteHost := Edit1.Text;
NMUDP1.RemotePort := StrToInt(Edit2.Text);
NMUDP1.SendBuffer(txt^,Length(Memo1.Text));
end;