delphi 7 serversocket clientsocket 文件传输 急急!!!(100)

  • 主题发起人 主题发起人 zhangxuepu107
  • 开始时间 开始时间
Z

zhangxuepu107

Unregistered / Unconfirmed
GUEST, unregistred user!
谁用过这两个控件,我这有个问题不知道怎么修改,欢迎加入讨论,加入者发源程序。QQ:85277206
 
1、用数据库:把.jpg文件存到表中,服务端随时读取查看;2、用Ftp方式上传、下载文件;3、你的想法也可以;...具体到百度上来一下,牛毛一样,祝你好运。[:)]
 
能不能具体指点一下啊。
 
baidu: TcpClient 文件传输以前下载过一个,地址不记得了。源代码也奉献给原先的公司了。
 
你可以用Socket传输,自己写传输协议,我记得以前的帖子有很多,查一下。客户端登陆服务端,建立一个线程(长连接),需要看客户端的文件,就发消息通知客户端索取列表。
 
我的最终结果是一台电脑可以任意看局域网其它电脑的文件。文件是抓屏的文件jpg.有做过这样的指教一下,最好把源码贴出!
 
抓屏后要转成tmemorystream吧?
 
现在问题是服务器端不需要客户端的干汲怎么获取文件?
 
我都不知道你问的到底是什么问题,被你弄糊涂了,如果你想抓图那么给你一个抓图函数,方框区域中抓图:{==============================================================================}{ use this to capture a rectangle on the screen... }function CaptureScreenRect( arect: trect ): tbitmap;{==============================================================================}var screendc: hdc;begin result := TBitmap.create; with result, arect do begin width := right - left; height := bottom - top; screendc := getdc( 0 ); try Bitblt( canvas.handle, 0, 0, width, height, screendc, left, top, srccopy ); finally Releasedc( 0, screendc ); end; end;end;如果你想tcp/ip通信那么你说的TidTCPServer和TidTcpClient控件完全可以做到文件传输,以下是个日期时间查询服务器的例子:服务器代码:uses IdSocketHandle;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);//var// Binding: TIDSocketHandle;begin With IdTCPServer1.Bindings.Add do begin IP:= '10.144.123.61'; Port:= 23; end; IdTCPServer1.Active:= true;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin IdTCPServer1.Active:= false;end;procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);begin //AThread.Connection.WriteLn('Connected server!');end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);var s: string;begin s:= AThread.Connection.ReadLn(); if SameText(s, 'Date') then AThread.Connection.WriteLn( DateToStr( Now ) ) else if SameText( s, 'Time' ) then AThread.Connection.WriteLn( TimeToStr( Now ) ); end;客户端查询日期时间的代码:procedure TForm1.Button1Click(Sender: TObject);begin with IdTCPClient1 do begin Host:= '10.144.123.61'; Port:= 23; try Connect; MessageDlg('连接成功!',mtInformation, [mbOK], 0); except MessageDlg('连接错误!',mtError, [mbOK], 0); end; end;end;procedure TForm1.Button2Click(Sender: TObject);begin IdTCPClient1.Disconnect;end;procedure TForm1.Button3Click(Sender: TObject);begin IdTCPClient1.WriteLn('Date'); ShowMessage( IdTCPClient1.ReadLn() );end;procedure TForm1.Button4Click(Sender: TObject);begin IdTCPClient1.WriteLn('Time'); ShowMessage( IdTCPClient1.ReadLn() );end;
 
你问TcpServer和TcpClient怎么用就行了.
 
TO:北京男人 你那有传输文件的例子吗?能不能留下你的QQ?
 
不需要客户端 = 没可能
 
我把源码给你,客户端unit Main;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls,WinSock, XPMan;type TFrmMain = class(TForm) OpenDFile: TOpenDialog; Progressbar1: TProgressBar; BitBegin: TBitBtn; BitStop: TBitBtn; BitExit: TBitBtn; Label1: TLabel; Label2: TLabel; StaBar: TStatusBar; Edit1: TEdit; Edit2: TEdit; XPManifest1: TXPManifest; procedure BitExitClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure BitBeginClick(Sender: TObject); procedure BitStopClick(Sender: TObject); private { Private declarations } public { Public declarations } StopTrans :Boolean; InTrans :Boolean; procedure CoveyFile(filename:String); end;var FrmMain: TFrmMain;implementation{$R *.DFM}procedure TFrmMain.BitExitClick(Sender: TObject);begin Close;end;procedure TFrmMain.FormCreate(Sender: TObject);var aWSAData : TWSAData;begin if WSAStartup($0101,aWSAData)<>0 then begin MessageBox(Handle,'不能启动winsock动态链接库!','错误',MB_OK); Exit; end;end;procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);var tim : Tdatetime;begin if InTrans then if MessageBox(Handle,'正在传输文件,停止吗?','提示',MB_YESNO)=IDNO then begin Action := caNone; Exit; end else begin StopTrans := True; tim := Now; while (Now-tim<0.0001) and InTrans do AppliCation.ProcessMessages; end; if WSACleanup<>0 then MessageBox(Handle,'清除WinSock动态链接库错误!','提示',MB_OK)end;procedure TFrmMain.CoveyFile(filename:String);Const BlockLen=1024*4;var Fby : File of Byte; FL : Integer; BlNum,ReLen : Integer; BlBuf : Array[0..BlockLen-1] of Byte; i : Integer; SL : Integer; Client : TSocket; ca : SOCKADDR_IN; Haddr : u_long;begin Client := Socket(PF_INET, SOCK_STREAM, IPPROTO_IP); if Client=INVALID_SOCKET then begin StaBar.SimpleText := '为连接远程服务器端创建Socket错误!' ; Exit; end; ca.sin_family := PF_INET; ca.sin_port := htons(StrToInt(Trim(Edit2.Text))); //获取端口 Haddr := inet_addr(PChar(Trim(Edit1.Text))); //获取IP地址 if (Haddr=-1) then begin StaBar.SimpleText := ' 主机IP地址 :'+Trim(Edit1.Text)+' 错误!' ; closesocket(Client); Exit; end else ca.sin_addr.S_addr := Haddr; if connect(Client,ca,sizeof(ca))<>0 then begin StaBar.SimpleText := '服务器连接错误!'; closesocket(Client); Exit; end else begin StaBar.SimpleText := '远程连接成功!' ; Progressbar1.Visible := True; end; AssignFile(Fby,filename); Reset(Fby); FL := FileSize(Fby); BlNum := FL div BlockLen; Progressbar1.Max := 1+BlNum; ReLen := FL mod BlockLen; StopTrans := False; InTrans := True; SL := 1; for i:=0 to BlNum-1 do begin if (StopTrans)or(SL<=0) then Break; BlockRead(Fby,BlBuf[0],BlockLen); SL := send(Client,BlBuf,BlockLen,0); Progressbar1.Position := i; Application.ProcessMessages; end; if StopTrans then begin CloseFile(Fby); closesocket(Client); InTrans := False; StaBar.SimpleText := ''; MessageBox(Handle,'停止传输!','提示',MB_OK); Progressbar1.Position := 0; Exit; end; if (SL<=0) then begin CloseFile(Fby); closesocket(Client); InTrans := False; StaBar.SimpleText := ''; MessageBox(Handle,'传输异常终止!','提示',MB_OK); Progressbar1.Position := 0; Exit; end; if ReLen>0 then begin BlockRead(Fby,BlBuf[0],ReLen); SL := send(Client,BlBuf,ReLen,0); if (SL<=0) then begin CloseFile(Fby); closesocket(Client); InTrans:=False; StaBar.SimpleText := ''; MessageBox(Handle,'传输异常终止!','提示',MB_OK); Progressbar1.Position := 0; Exit; end; end; Progressbar1.Position := Progressbar1.Max; CloseFile(Fby); closesocket(Client); InTrans := False; StaBar.SimpleText := ''; MessageBox(Handle,'传输完成!','提示',MB_OK); Progressbar1.Position := 0;end;procedure TFrmMain.BitBeginClick(Sender: TObject);begin if (OpenDfile.Execute)and (FileExists(OpenDfile.FileName)) then CoveyFile(OpenDfile.FileName);end;procedure TFrmMain.BitStopClick(Sender: TObject);begin StopTrans := True;end;end.服务器端unit Main;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls,WinSock, XPMan;type TFrmMain = class(TForm) BitBegin: TBitBtn; BitStop: TBitBtn; BitExit: TBitBtn; Label2: TLabel; StatusBar1: TStatusBar; BitListen: TBitBtn; Edit2: TEdit; XPManifest1: TXPManifest; SaveDialog1: TSaveDialog; procedure BitExitClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure BitBeginClick(Sender: TObject); procedure BitStopClick(Sender: TObject); procedure BitListenClick(Sender: TObject); private { Private declarations } public { Public declarations } StopTrans :Boolean; InTrans :Boolean; Server :TSocket; procedure MeetFile(filename:String); end;var FrmMain: TFrmMain;implementation{$R *.DFM}procedure TFrmMain.BitExitClick(Sender: TObject);begin Close;end;procedure TFrmMain.FormCreate(Sender: TObject);var aWSAData:TWSAData;begin if WSAStartup($0101,aWSAData)<>0 then begin MessageBox(Handle,'不能启动WinSock动态链接库!','错误',MB_OK); Exit; end;end;procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);var tim:Tdatetime;begin if InTrans then if MessageBox(Handle,'正在传输文件,是否退出?','提示',MB_YESNO)=IDNO then begin Action:=caNone; Exit; end else begin StopTrans:=True; tim:=Now; while (Now-tim<0.0001) and InTrans do AppliCation.ProcessMessages; end; if Server<>INVALID_SOCKET then closesocket(Server); if WSACleanup<>0 then MessageBox(Handle,'清除WinSock动态链接库错误!','提示',MB_OK)end;procedure TFrmMain.MeetFile(filename:String);Const BlockLen=1024*4;var Fby : File of Byte; ReL : Integer; Buf : Array[0..BlockLen-1] of Byte; Rket : TSocket; ra : SOCKADDR_IN; ralen : Integer;begin ralen := sizeof(Ra); Rket:=accept(Server,@ra,@ralen); AssignFile(Fby,filename); ReWrite(Fby); StopTrans := False; InTrans := True; ReL := recv(Rket,Buf,BlockLen,0); while (ReL>0) and (not StopTrans) do begin BlockWrite(Fby,Buf[0],ReL); Application.ProcessMessages; ReL := recv(Rket,Buf,BlockLen,0); end; if StopTrans then begin CloseFile(Fby); closesocket(Rket); InTrans := False; MessageBox(Handle,'停止传输!','提示',MB_OK); Exit; end; CloseFile(Fby); closesocket(Rket); InTrans := False; if (ReL=SOCKET_ERROR) then MessageBox(Handle,'传输异常终止!','提示',MB_OK)end;procedure TFrmMain.BitBeginClick(Sender: TObject);begin if (Server=INVALID_SOCKET) then begin MessageBox(Handle,'请先进行监听!','提示',MB_OK); Exit; end; if SaveDialog1.Execute then MeetFile(SaveDialog1.FileName);end;procedure TFrmMain.BitStopClick(Sender: TObject);var tim:Tdatetime;begin StopTrans := True; tim := Now; while (Now-tim<0.0001) and InTrans do AppliCation.ProcessMessages; if Server<>INVALID_SOCKET then closesocket(Server); Server := INVALID_SOCKET; BitStop.Enabled := False; BitListen.Enabled := True;end;procedure TFrmMain.BitListenClick(Sender: TObject);var ca : SOCKADDR_IN;begin Server := Socket(PF_INET, SOCK_STREAM, IPPROTO_IP); if Server=INVALID_SOCKET then begin StatusBar1.SimpleText := '创建接收错误!' ; Exit; end; ca.sin_family := PF_INET; ca.sin_port := htons(StrToInt(Trim(Edit2.Text))); ca.sin_addr.S_addr := INADDR_ANY; if bind(Server,ca,sizeof(ca))=SOCKET_ERROR then begin StatusBar1.SimpleText := '绑定接收端错误!请更改接收端口!'; closesocket(Server); Exit; end else StatusBar1.SimpleText := '绑定接收端成功!'; listen(Server,5); BitListen.Enabled := False; BitStop.Enabled := True;end;end.
 
呵呵,大家有好东西,都共享一下,不要藏着
 
火焰领主打伞 同志 不辞幸苦。有劳了。
 
TO:火焰领主打伞用过serversocket clientsocket吗?我现在用这两个写的,传一个文件可以,不过,当有一个文件列表,循环传文件时就传不过去?也测不出什么问题?谁做的过的话,我可以把源码发过去。QQ:85277206
 

Similar threads

S
回复
0
查看
835
SUNSTONE的Delphi笔记
S
S
回复
0
查看
765
SUNSTONE的Delphi笔记
S
D
回复
0
查看
824
DelphiTeacher的专栏
D
后退
顶部