我想在局域网传输大文件(100分)

  • 主题发起人 主题发起人 fanronghua
  • 开始时间 开始时间
F

fanronghua

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在局域网传输大文件,怎么弄啊,用ClientSocket serverSocket!请高手赐教!!
 
有什么不同吗?那就一兆一兆的传输就是了
 
能不能给出例子
 
unit Unit1;//客户端
interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
StatusBar1: TStatusBar;
OpenDialog1: TOpenDialog;
ClientSocket1: TClientSocket;
Button2: TButton;
ProgressBar1: TProgressBar;
Button3: TButton;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
stream : TFileStream; //定义内存流,其实用文件流也可以 TMemoryStream
size,count : int64; //文件大小

{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

const
_IniFileName = 'Client.ini';
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.host := Edit1.Text;
ClientSocket1.Active := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
stream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
//stream.LoadFromFile(OpenDialog1.FileName); 速度太慢
size := stream.Size; //获得文件大小
count := 0;
ProgressBar1.Max := size;
ClientSocket1.Socket.SendText(inttostr(size) +'-'+ExtractFileName(OpenDialog1.FileName)); //先发送大小和文件名
end;
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.Panels[0].Text := '已连接:' + Socket.RemoteAddress;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
rcvtxt : string;
buf : array [1..2048] of char;
left : integer;
begin
rcvtxt := Socket.ReceiveText;
left := size - count; //未读字节数
if AnsiPos('go',rcvtxt)=1 then //收到响应则发
begin
ProgressBar1.Position := count;
if sizeof(buf)<left then //若满一块则读一块
begin
stream.Read(buf,sizeof(buf));
Socket.SendBuf(buf,sizeof(buf));
count := count + sizeof(buf);
end
else //不满则发剩余的
begin
stream.Read(buf,left);
Socket.SendBuf(buf,left);
FreeAndNil(stream);
end;
end
end;

end.
 
怎么没有接收?
 
接收端
procedure TFormServer.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
buf: array [1..2048] of char;
rcvtxt, FName: string;
left, IPIndex: integer;
begin
IPIndex := IPSL.IndexOf(Socket.RemoteAddress);
if IPIndex < 0 then
Exit;
try //要控制客户端发完了文件后才能再发送其它信息,否则认为是文件信息,会被写入文件
if FileRec[IPIndex].FileName = '' then
begin
rcvtxt := Socket.ReceiveText;
if Copy(rcvtxt, 1, Length(_GetFileName)) = _GetFileName then //取文件名
begin
FName := Copy(rcvtxt, Pos('-', rcvtxt) + 1, Length(rcvtxt));
if FileExists(SavePath + FName) then
Socket.SendText(_FileExists) //发送文件存在标志
else
Socket.SendText(_NotFileExists);//发送文件不存在标志
end
else //发送文件名和大小
begin
FileRec[IPIndex].Size := StrToIntDef(Copy(rcvtxt, 1, Pos('-', rcvtxt) - 1), 0);
FileRec[IPIndex].Count := 0;
if Not DirectoryExists(SavePath) then
ForceDirectories(SavePath);
FileRec[IPIndex].FileName := SavePath + copy(rcvtxt, Pos('-', rcvtxt) + 1, Length(rcvtxt));
AssignFile(FileRec[IPIndex].TargetFile, FileRec[IPIndex].FileName);
Rewrite(FileRec[IPIndex].TargetFile, 1);
FileRec[IPIndex].LinesIndex := MemoReport.Lines.Count - 1;
Socket.SendText(_AllowSend); //响应一块
end;
end
else
begin
left := FileRec[IPIndex].Size - FileRec[IPIndex].Count; //未写的字节数
if sizeof(buf) < left then //满一块则写一块
begin
Socket.ReceiveBuf(buf, sizeof(buf));
BlockWrite(FileRec[IPIndex].TargetFile, buf, sizeof(buf));
FileRec[IPIndex].Count := FileRec[IPIndex].Count + sizeof(buf);
Socket.SendText(_AllowSend);
end
else //否则写剩余字节数
begin
Socket.ReceiveBuf(buf,left);
BlockWrite(FileRec[IPIndex].TargetFile,buf,left);
CloseFile(FileRec[IPIndex].TargetFile);
FileRec[IPIndex].FileName := '';
end;
end;
except
end;
end;
 
定义一个协议再传。
 
不要意思, IPIndex := IPSL.IndexOf(Socket.RemoteAddress);
是什么意思啊?
IPSL 是什么?
 
zfsfj 先给你50分!还有50分谁要?
 
看看这个软件
http://www.csdn.net/cnshare/soft/15/15120.shtm
 
我给你发一个昨天刚写的!!给我邮箱。我是用SOCKET API写的。
 
leaber:
我也要。Ming_cn@163.com
 
3q~~~~ leaber
我的邮箱 fanronghua__@163.com
 
楼上两位已发!!!查收。
 
可以发给我一份吗?谢谢!
xumin23@sina.com
xumin23@sohu.com
 
我也要 谢谢
juanke@163.com
 

leaber 50

zfsfj 50


 
leaber 50
zfsfj 50
 

Similar threads

后退
顶部