如何利用DELPHI的 Tseversocket Tclientsocket 实现一个bmp文件的传输?急需答复!谢!

shaokun305 创始人  2003-01-04 08:02:54
如何利用DELPHI的 Tseversocket Tclientsocket 实现一个bmp文件的传输?急需答复!谢!
...全文
6 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangrenguang 2003-01-12
服务端程序:
unit Unit1;
//By mincer 2001
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;

type
TForm1 = class(TForm)
Ser1: TServerSocket;
procedure Ser1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
// procedure Ser1Accept(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
//filename:string;
STARTread:boolean;
f:file;
cmd:string;
Filename:string;
//getFilenamemask:boolean;
implementation

{$R *.DFM}


procedure TForm1.Ser1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var b:byte;
r:integer;
begin

case socket.ReceiveLength of
3: begin
cmd:=socket.ReceiveText;
if cmd='GET' then
socket.SendText('OKR')
else if cmd='LYF' then
close
else if cmd='OKS' then
begin
assignfile(f,filename);
reset(f,1);
blockread(f,b,1,r);
socket.SendBuf(b,1)
end
else if cmd='OKG' then
begin
blockread(f,b,1,r);
if r=0 then
begin
closefile(f);
socket.SendText('END');
end
else
socket.SendBuf(b,1);
end;
end;
4..255:begin
filename:=socket.ReceiveText;
socket.SendText('OKN');
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ser1.Port:=8372;
ser1.Active:=true;
end;

end.
回复
huangrenguang 2003-01-12
客户端程序:
type
TForm1 = class(TForm)
Cli1: TClientSocket;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
SD1: TSaveDialog;
Edit2: TEdit;
Edit3: TEdit;
Label2: TLabel;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Cli1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure Cli1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
f:file;
cmd:string;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
cli1.Address:=edit2.Text;
//cli1.Port:=IntToStr(Edit3.Text);
cli1.Active:=true;
button1.Enabled:=false;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if sd1.Execute then
begin

assignfile(f,sd1.FileName);
reset(f,1);
cli1.Socket.SendText(edit1.text);
end;
end;


procedure TForm1.Cli1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
showmessage('error');
end;

procedure TForm1.Cli1Read(Sender: TObject; Socket: TCustomWinSocket);
var b:byte;
begin
case socket.ReceiveLength of
1:begin
socket.ReceiveBuf(b,1);
blockwrite(f,b,1);
socket.SendText('OKG');
end;
3: begin
cmd:=socket.ReceiveText;
if cmd='END' then
begin
closefile(f);
showmessage('下载完成!')
end
else if cmd='OKR' then
socket.SendText('OKS')
else if cmd='OKN' then
cli1.Socket.SendText('GET');
end;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
messagebox(0,'欢迎光临mincer之家------http://mincer.yeah.net'+#13+'本程序没有加很多错误处理,各位可以加上^_^','Information',64);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
cli1.Socket.SendText('LYF');
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
close;
end;

end.
回复
Jiasong 2003-01-07
可用stream传输,delphi的demo有例子
回复
发动态
发帖子
网络通信/分布式开发
创建于2007-08-02

1565

社区成员

Delphi 网络通信/分布式开发
申请成为版主
社区公告
暂无公告