一个很简单的问题(50分)

  • 主题发起人 主题发起人 jinmen
  • 开始时间 开始时间
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
txt:pchar;
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;




 
我用内存流来实现UDP方式的收发数据。发送接收字节数到640K也没问题,
以下是我的代码,我省略了很多。

TUDPThread = class(TThread)
private
{ Private declarations }
FScadaDatagram: TScadaDatagram;
FTmpStream: TMemoryStream;
FUDP: TNMUDP;
FCommBuffer: String;
procedure OnUDPBufferInvalid(var Handled: Boolean; var Buff: Array of Char;
var Length: Integer);
procedure OnUDPDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String;
Port: Integer);
procedure OnUDPDataSend(Sender: TObject);
procedure OnUDPInvalidHost(var Handled: Boolean);
procedure OnUDPStatus(Sender: TComponent; Status: String);
procedure OnUDPStreamInvalid(var handled: boolean; Stream: TStream);
protected
procedure Execute; override;
function ReadDataStream(NumberBytes: Integer): String;
procedure SendDataStream(Data: String);
public
constructor Create;
destructor Destroy; override;

procedure HandleUDPDatagram(Data: String);
procedure DisplayUDPCommBuffer;
procedure DisplayScadaDataTime;
property UDP: TNMUDP read FUDP write FUDP;
property ScadaDatagram: TScadaDatagram read FScadaDatagram write FScadaDatagram;
end;

constructor TUDPThread.Create;
begin
inherited Create(True);

FScadaDatagram := TScadaDatagram.Create;

FUDP := TNMUDP.Create(nil);
FUDP.RemotePort := 55550;
FUDP.LocalPort := 55550;
FUDP.RemoteHost := '10.147.214.255';

FUDP.OnBufferInvalid := OnUDPBufferInvalid;
FUDP.OnDataReceived := OnUDPDataReceived;
FUDP.OnDataSend := OnUDPDataSend;
FUDP.OnInvalidHost := OnUDPInvalidHost;
FUDP.OnStatus := OnUDPStatus;
FUDP.OnStreamInvalid := OnUDPStreamInvalid;

FTmpStream := TMemoryStream.Create;
end;

procedure TUDPThread.OnUDPDataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
var
sData: String;
begin
sData := ReadDataStream(NumberBytes);
HandleUDPDatagram(sData);
end;

function TUDPThread.ReadDataStream(NumberBytes: Integer): String;
var
RecvStr: String;
begin
FUDP.ReadStream(FTmpStream);
SetLength(RecvStr, NumberBytes);
FTmpStream.Read(RecvStr[1], NumberBytes);
FTmpStream.Clear;
Result := RecvStr;
end;

procedure TUDPThread.SendDataStream(Data: String);
var
SendStr: String;
begin
SendStr := Data;
FTmpStream.Write(SendStr[1], Length(SendStr));
FUDP.SendStream(FTmpStream);
FTmpStream.Clear;
end;
 
谢谢你,不过我只是想问我采用的数据报方式什么地方有问题啊,因为即使不是流方式,也应该可以发送超过四个字节啊!
 
接受答案了.
 
后退
顶部