S
shchen
Unregistered / Unconfirmed
GUEST, unregistred user!
就是传输图象文件的问题。我定义了一个包的结构:Packstruct=record
packid:byte;//第几包
commandid:byte;
packcontent:array[0..packlen]of char;//包的内容
flag:boolean;//为true的时候表示没有下一个包了;为false 表示还有下个包
每次发包长度为1500,用nmudp实现发送和接收
发送图象:
procedure TForm1.Button3Click(Sender: TObject);
var
f:file;
len:longint;
i,n:longint;
NumRead, NumWritten: Integer;
outbuf,inbuf:array[0..1500] of char;
sendbuf:array[0..packlen]of char;
returnlen:integer;
begin
if opendialog1.Execute then
begin
sendpicture.RemoteHost:=teacherip;
assignfile(f,opendialog1.FileName);
len:=Fsize(opendialog1.FileName);
n:=len div 1500;
if n=0 then
begin
sendpack.packid:=1;
Blockread(f,inbuf,sizeof(inbuf),numread);
copymemory(@(sendpack.packcontent[0]),@(inbuf[0]),sizeof(inbuf));
sendpack.flag:=true;
copymemory(@(outbuf[0]),@sendpack,sizeof(sendpack));
sendpicture.SendBuffer(outbuf,sizeof(sendpack));
end
else if (len mod 1500)=0 then
begin
for i:=1 to n do
sendpack.packid:=i;
blockread(f,inbuf,sizeof(inbuf),numread);
copymemory(@(sendpack.packcontent[0]),@(inbuf[0]),sizeof(inbuf));
sendpack.flag:=false;
if i=n then begin
sendpack.flag:=true;
end;
copymemory(@(outbuf[0]),@sendpack,sizeof(sendpack));
sendpicture.SendBuffer(outbuf,sizeof(sendpack));
end
else if(len mod 1500)<>0
then begin
for i:=1 to n+1 do
sendpack.packid:=i;
blockread(f,inbuf,sizeof(inbuf),numread);
copymemory(@(sendpack.packcontent[0]),@(inbuf[0]),sizeof(inbuf));
sendpack.flag:=false;
if i=n+1 then begin
sendpack.flag:=true;
end;
copymemory(@(outbuf[0]),@sendpack,sizeof(sendpack));
sendpicture.SendBuffer(outbuf,sizeof(sendpack));
end;
接收端不知道如何收啊?如何把收到的包放在一个缓冲,然后用image 显示出来呢?
我写了点代码如下:
procedure TForm1.receivepictureDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
NumRead, NumWritten: Integer;
outbuf,inbuf:array[0..udpcmdlen] of char;
len:integer;
f:file;
begin
repeat
receivepicture.Readbuffer(inbuf,len);
CopyMemory(@receivepack,@(inbuf[0]),sizeof(receivepack));
blockwrite(f,inbuf,numwritten);
until receivepack.flag=true ;
如何在image 中显示呢????
end;
高手请指教一下吧!!
packid:byte;//第几包
commandid:byte;
packcontent:array[0..packlen]of char;//包的内容
flag:boolean;//为true的时候表示没有下一个包了;为false 表示还有下个包
每次发包长度为1500,用nmudp实现发送和接收
发送图象:
procedure TForm1.Button3Click(Sender: TObject);
var
f:file;
len:longint;
i,n:longint;
NumRead, NumWritten: Integer;
outbuf,inbuf:array[0..1500] of char;
sendbuf:array[0..packlen]of char;
returnlen:integer;
begin
if opendialog1.Execute then
begin
sendpicture.RemoteHost:=teacherip;
assignfile(f,opendialog1.FileName);
len:=Fsize(opendialog1.FileName);
n:=len div 1500;
if n=0 then
begin
sendpack.packid:=1;
Blockread(f,inbuf,sizeof(inbuf),numread);
copymemory(@(sendpack.packcontent[0]),@(inbuf[0]),sizeof(inbuf));
sendpack.flag:=true;
copymemory(@(outbuf[0]),@sendpack,sizeof(sendpack));
sendpicture.SendBuffer(outbuf,sizeof(sendpack));
end
else if (len mod 1500)=0 then
begin
for i:=1 to n do
sendpack.packid:=i;
blockread(f,inbuf,sizeof(inbuf),numread);
copymemory(@(sendpack.packcontent[0]),@(inbuf[0]),sizeof(inbuf));
sendpack.flag:=false;
if i=n then begin
sendpack.flag:=true;
end;
copymemory(@(outbuf[0]),@sendpack,sizeof(sendpack));
sendpicture.SendBuffer(outbuf,sizeof(sendpack));
end
else if(len mod 1500)<>0
then begin
for i:=1 to n+1 do
sendpack.packid:=i;
blockread(f,inbuf,sizeof(inbuf),numread);
copymemory(@(sendpack.packcontent[0]),@(inbuf[0]),sizeof(inbuf));
sendpack.flag:=false;
if i=n+1 then begin
sendpack.flag:=true;
end;
copymemory(@(outbuf[0]),@sendpack,sizeof(sendpack));
sendpicture.SendBuffer(outbuf,sizeof(sendpack));
end;
接收端不知道如何收啊?如何把收到的包放在一个缓冲,然后用image 显示出来呢?
我写了点代码如下:
procedure TForm1.receivepictureDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
NumRead, NumWritten: Integer;
outbuf,inbuf:array[0..udpcmdlen] of char;
len:integer;
f:file;
begin
repeat
receivepicture.Readbuffer(inbuf,len);
CopyMemory(@receivepack,@(inbuf[0]),sizeof(receivepack));
blockwrite(f,inbuf,numwritten);
until receivepack.flag=true ;
如何在image 中显示呢????
end;
高手请指教一下吧!!