各位请帮我找找用SOCKET编的能在网络传输文件的实例好吗?我找了很久,都没有找到,虽然分数少?希望大家能帮忙。
解决方案 »
- delphi中的image中的图片问题!!
- Delphi数据库操作-如何精确删除数据
- 数据库连接超时的问题
- Dephi7中使用QuickReport为何总出PrinterSettings.ExtendedDuplex错?
- TdxfLabel,TdxfPictureButton是哪个第三方控件里的类(在线等待.....)
- 使用ADOQUERY时的SQL语句参数传递问题,大家帮我看看!
- 如何让image的图象旋转90度或180?
- 下载了一个第三方控件,带源码,怎样更改其中的英文字符串为中文?
- 100元人民币 + 100分 求 (用 ACM控件录制 生成 WAV 音频文件)
- adoquery1.post 之后,取当前的纪录值,为什么取到的还是前一个纪录.
- 如何实现拖拉一个按扭到另一个位置?(在线等待)
- 高分寻找gexperts 1.12 for d6 (它的官方网站下不了)
[email protected]
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, NMStrm,
ExtDlgs, StdCtrls, Psock, ExtCtrls, ComCtrls;type
TForm1 = class(TForm)
Panel1: TPanel;
Image1: TImage;
Button1: TButton;
Button2: TButton;
NMStrm1: TNMStrm;
NMStrmServ1: TNMStrmServ;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
OpenPictureDialog1: TOpenPictureDialog;
StatusBar1: TStatusBar;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
strm: TStream);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure NMStrm1MessageSent(Sender: TObject);
procedure NMStrm1Connect(Sender: TObject);
procedure NMStrm1Disconnect(Sender: TObject);
procedure NMStrm1HostResolved(Sender: TComponent);
procedure NMStrm1Status(Sender: TComponent; Status: String);
procedure NMStrm1PacketSent(Sender: TObject);
procedure NMStrm1InvalidHost(var handled: Boolean);
procedure NMStrm1ConnectionFailed(Sender: TObject);
procedure NMStrmServ1ClientContact(Sender: TObject);
procedure NMStrmServ1Status(Sender: TComponent; Status: String);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}//发送文件
procedure TForm1.Button1Click(Sender: TObject);
var
MyFStream: TFileStream;
begin
If OpenDialog1.Execute then
Begin
NMStrm1.Host := Edit2.Text;
NMStrm1.FromName := Edit1.Text;
MyFStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
try
NMStrm1.PostIt(MyFStream);
finally
MyFStream.Free;
end;
end;
end;//接受文件
procedure TForm1.NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
strm: TStream);
var
MyFStream: TFileStream;
begin
If FileExists(edit1.text) then DeleteFile(edit1.text);
MyFStream := TFileStream.Create(edit1.text, fmCreate);
try
MyFStream.CopyFrom(strm, strm.size);
finally
MYFStream.Free;
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
// Image1.Picture.LoadFromFile(edit1.text);
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If FileExists('.\tmp.bmp') then DeleteFile('.\tmp.bmp');
end;procedure TForm1.NMStrm1MessageSent(Sender: TObject);
begin
ShowMessage('Stream Sent');
end;procedure TForm1.NMStrm1Connect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Connected';
end;procedure TForm1.NMStrm1Disconnect(Sender: TObject);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := 'Disconnected';
end;procedure TForm1.NMStrm1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'Host Resolved';
end;procedure TForm1.NMStrm1Status(Sender: TComponent; Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;procedure TForm1.NMStrm1PacketSent(Sender: TObject);
begin
StatusBar1.SimpleText := IntToStr(NMStrm1.BytesSent)+' of '+IntToStr(NMStrm1.BytesTotal)+' sent';
end;procedure TForm1.NMStrm1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
Begin
NMStrm1.Host := TmpStr;
Handled := TRUE;
End;
end;procedure TForm1.NMStrm1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;procedure TForm1.NMStrmServ1ClientContact(Sender: TObject);
begin
NMStrmServ1.ReportLevel := Status_Basic;
NMStrmServ1.TimeOut := 90000;
StatusBar1.SimpleText := 'Client connected';
end;procedure TForm1.NMStrmServ1Status(Sender: TComponent; Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;end.DELPHI自带的例子修改而成(FASTNET-STRM)
var
F: file;
size:integer;
name,filename:string;
temp:string;
allbuf:pchar;
begin
filename:=path+text;
name:=text;
AssignFile(F,filename);
Reset(F, 1);
Size := FileSize(F);
//文件总长+信息类别+文件名长+文件长+文件名+文件
allbuf:=(allocmem(9+2+3+8+length(name)+size+1));
temp:=inttostr(9+2+3+8+length(name)+size+1);
while length(temp)<9 do
temp:='0'+temp;
strcopy(allbuf,StrToPch(temp)); //写入总长度
strcopy(allbuf+9,strtopch(zl)); //写入信息类别
temp:=inttostr(length(name));
while length(temp)<3 do
temp:='0'+temp;
strcopy(allbuf+11,StrToPch(temp));//写入文件名长(不带路径)
temp:=inttostr(size);
while length(temp)<8 do
temp:='0'+temp;
strcopy(allbuf+14,StrToPch(temp));//写入文件长
strcopy(allbuf+22,StrToPch(name));//写入文件名
BlockRead(F, (allbuf+22+length(name))^, Size,size);//写入文件
CloseFile(F);
Socket.Sendbuf((allbuf)^,9+2+3+8+length(name)+size+1);
FreeMem(allbuf);
end;接收文件 :
procedure TfrmMain.RecieveFile(path:string;index:integer;lb:string;var tbbz:boolean);
var
temp,filename,pathname:string;
F:file;
begin
try
filename:=copy(globalbuf.SoketList[index].buf,12,3); //读取文件名长度
filename:=copy(globalbuf.SoketList[index].buf,23,strtoint(filename));//读取文件名
pathname:=path+filename; //带路径的文件名
AssignFile(F,pathname);
rewrite(f,1);
temp:=copy(globalbuf.SoketList[index].buf,15,8); //读取文件长度
//写文件
BlockWrite(F, (globalbuf.SoketList[index].buf+22+length(filename))^, strtoint(temp));
CloseFile(F);
except end;
end;
中国软件开发网络 --> 开发图书馆 --> Delphi --> Internet --> 用Delphi编写点对点传文件程序
关键字:
用Delphi编写点对点传文件程序 贴文时间
2001-8-19 18:49:08 文章类型:
转贴
ghj1976 转贴 出处: http://www.china-pub.com/computers/emook/0236/info.htm
文章摘要:
Delphi功能强大,用Delphi写软件,可以大大缩短软件的开发周期。本文介绍怎样用Delphi编写点对点传文件程序。
--------------------------------------------------------------------------------
用Delphi编写点对点传文件程序 Delphi功能强大,用Delphi写软件,可以大大缩短软件的开发周期。关于点对点传文件的基本思路,就是一个服务器软件,一个客户端软件,使用同一个端口,待连接上以后,客户端给服务器发送一个请求,包括待传的文件的文件名,大小等,如果服务器接受,就开始传文件。当然,文件传输的时候可以有两种模式,ASCII码和Bin,不过一般通用Bin 就可以了。基于上面的讨论,本来用Delphi4的NMStrm,NMStrmServ 控件就可以完成,但是我测试过了,NMStrm控件对于较小的文件还可以使用,而且很方便,但是如果文件一大(1M)就会出错。所以接下来我们利用Delphi中TServerSocket和TClientSocket写这个程序由于以太包大小的限制以及DelphiSocket的处理机制(Delphi中,当你用一个Socket发送一个较大的Stream,接受方会激发多次OnRead事件,Delphi她只保证多次OnRead事件中每次数据的完整,而不会自己收集数据并返回给用户。所以不要以为你把待传文件在一个Socket中Send一次,另一个中Recv一次就可以了。你必须自己收集数据或自己定义协议。),所以我们采用自定义协议的方法。定义协议的规范方法是利用Record End。如:
TMyFileProtocol=Record
sSendType=(ST_QUERY,ST_REFUSE,ST_DATA,ST_ABORT,...);
iLength:integer;
bufSend:Buffer;
End;
我曾试过这个办法,但失败了,而且我一直认为我的方法是正确的,但程序一直编译通不过,估计是Delphi有问题:) 所以我在下列的范例程序中利用另外一种办法。Socket 类中有两属性ReceiveText和ReceiveBuf,在一个OnRead事件中,只能使用一次该两属性,所以我们可以利用一个全程变量来保存是该读Text还是Buf,也就是说读一次Text,再都一次Buf,这就模拟了TMyFileProtocol。
开始程序:
写一个最简单的,主要用于讲解方法。
定义协议:
Const
MP_QUERY ='1';
MP_REFUSE ='2';
MP_ACCEPT ='3';
MP_NEXTWILLBEDATA='4';
MP_DATA ='5';
MP_ABORT ='6';
MP_OVER ='7';
MP_CHAT ='8';协议简介:
首先由Client发送MP_QUERY,Server接受到后发送MP_ACCEPT或MP_FEFUESE;
Client接受到MP_ACCEPT发送MP_FILEPROPERTY,Server接受到后发送MP_NEXTWILLBEDATA;
Client接受到发送MP_NEXTWILLBEDATA,Server接受到后发送MP_DATA;
Client接受到MP_DATA,发送数据,Server接受数据,并发送MP_NEXTWILLBEDATA;
循环,直到Client发送MP_OVER;
中间可以互相发送MP_CHAT+String; Server程序:
放上以下控件:SaveDialog1,btnStartServer,
ss,(TServerSocket)btnStartServer.OnClick(Sender:TObject);
begin
ss.Port:=2000;
ss.Open;
end;ss.OnClientRead(Sender: TObject;Socket: TCustomWinSocket);
var
sTemp:string;
bufRecv:Pointer;
iRecvLength:integer;
begin
if bReadText then
begin
sTemp:=Socket.ReceiveText;
case sTemp[1] of
MP_QUERY:begin
//在这里拒绝
SaveDialog1.FileName:=Copy(sTemp,2,Length(STemp));
if SaveDialog1.Execute then
begin
Socket.SendText(MP_ACCEPT);
fsRecv:=TFileStream.Create(SaveDialog1.FileName,fmCreate);
end
else Socket.SendText(MP_REFUSE+'去死');
end;
MP_FILEPROPERTY:begin
//要发送StrToInt(Copy(sTemp,2,Length(sTemp))) 次
//时间进度显示
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_NEXTWILLBEDATA:begin
Socket.SendText(MP_DATA);
bReadText:=false;
end;
MP_END:begin
fsRecv.Free
bReadText:=true;
end;
MP_ABORT:begin
fsRecv.Free;
bReadText:=true;
end;
MP_CHAT:begin
//Chat Msg
end;
end;{of case}
end
else begin
try
GetMem(bufRecv,2000);//2000 must >iBYTESEND
Socket.ReceiveBuf(bufRecv^,iRecvLength);
fsRecv.WriteBuffer(bufRecv^,iRecvLength);
finally
FreeMem(bufRecv,2000);
end;{of try}
bReadText:=true;
Socket.SendText(MP_NEXTWILLBEDATA);
end;
end;Client程序:
放上以下控件:edtIPAddress,OpenDialog1,btnConnect,btnSendFile,
cs. (TClientSocket)btnConnect.OnClick(Sender:TObject);
begin
cs.Address:=edtIPAddress.Text;
cs.Port:=2000;
cs.Connect;
end;btnSendFile.OnClick(Sender:TObject);
begin
if OpenDialog1.Execute then
Begin
cs.Socket.SendText(MP_QUERY+OpenDialog1.FileName);//FileSize???
end;
end;cs.OnRead(Sender: TObject;Socket: TCustomWinSocket);
var
sTemp:string;
bufSend:pointer;
begin
sRecv:=Socket.ReceiveText;
Case sRecv[1] of
MP_REFUSE:ShowMessage('Faint,be refused!');
MP_ACCEPT:begin
fsSend:=TFileStream.Create(OpenDialog1.FileName,fmOpen);
//iBYTEPERSEND是个常量,每次发送包的大小。
Socket.SendText(MP_FILEPROPERTY+Trunc(fsSend.Size/iBYTEPERSEND)+1);
end;
MP_NEXTWILLBEDATA:begin
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_DATA:begin
try
GetMem(bufSend,iBYTEPERSEND+1);
if (fsSend.Position+1+iBYTEPERSEND) < fsSend.Size then
begin
fsSend.Read(bufSend^,iBYTEPERSEND);
Socket.SendBuf(bufSend^,iBYTEPERSEND);
fsSend.Free;
end//普通的发送,大小为iBYTEPERSEND
else begin
fsSend.Read(bufSend^,fsSend.Size-fsSend.Position-1);
Socket.SendBuf(bufSend^,fsSend.Size-fsSend.Position-1);
end;//最后一次发送,发送剩余的数据
finally
FreeMem(bufSend,iBYTEPERSEND+1);
end;{of try}
end;
MP_ABORT:begin
//被取消了:(
fsSend.Free;
end;
end;{of case}
end;
整理程序:
加入错误判断,优化程序,把Server和Client联合在一起,加入剩余时间进度显示,做成能一次传多个文件,加入聊天功能,就成了一个很好的点对点传文件的程序。