那位给个socket文件传输的例程(所有的分)(160分)

  • 主题发起人 主题发起人 lvzhijong
  • 开始时间 开始时间
L

lvzhijong

Unregistered / Unconfirmed
GUEST, unregistred user!
两台机子之间实现文件传输(约2M)
1、硬件上如何连接?两机都有网卡,系统是win2k
2、软件如何规划?我的设想是,server端,先送文件长度,再将文件作成一个stream,
socket.sendstream。client端,接受字节数等于文件长度即结束。但是,如何将
接收到的数据回复为文件呢?
那位出出主意,只要在我的系统基础(两机都有网卡,系统是win2k)上实现
我的要求(两台机子之间实现文件传输(约2M))即可。
eamil:rdq.pzq@263.net


 
补充一下:两机连成一个局域网,不连internet。
 
如果你是将2M的文件做成一个Stream,发送一次的话,那么在接受的时候,创建文件STream,
然后将发送来的流存入此Stream中,即可
 
unit Unit1;

interface

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

type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;

PCON = ^TCON;

TForm1 = class(TForm)
SS: TServerSocket;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure SSClientConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure SSClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
SS.Port := 9000;
SS.Active := True;
end;

procedure TForm1.SSClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var c : pcon;
begin

c :=new(pcon);
c.FileName := '';
c.TotalSize := 0 ;
c.Status := 0;
Socket.Data := c;
Socket.SendText('已经连接,请输入UPLOAD FILENAME SIZE'#13#10);

end;

procedure TForm1.SSClientRead(Sender: TObject; Socket: TCustomWinSocket);
var C : PCON;
cmd:String;
Buffer : pointer;
nRetr : integer;
fs : TFileStream;
const bufferSize = 1024 ;

begin
C:= Socket.Data ;
case c.Status of
0 :
begin
cmd := trim(Socket.ReceiveText) ;

if Pos('UPLOAD ',uppercase(cmd)) > 0 then
begin
c.FileName := trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
c.TotalSize := StrToInt(Copy(c.FileName,Pos(' ',c.FileName)+1,Length(c.FileName)));
c.FileName := trim(Copy(c.FileName,1,Pos(' ',c.FileName)));
c.Status := 1;
Socket.Data := C;
Socket.SendText('you can send File !'#13#10);
end;
end;
1 : begin
GetMem(Buffer,BufferSize);
nRetr := Socket.ReceiveBuf(Buffer^,BufferSize);

if not FIleExists('c:/'+c.FileName) then
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end
else
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;

fs.WriteBuffer(Buffer^,nRetr);

fs.Destroy;
FreeMem(Buffer);
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.Show;
end;

end.





<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
unit Unit2;

interface

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

type
TForm2 = class(TForm)
CS: TClientSocket;
OpenDialog1: TOpenDialog;
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
SendCommand: TButton;
Label1: TLabel;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure SendCommandClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure CSRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.DFM}
function GetFileSize(const FileName: string):integer;
var f : TFileStream;
begin
f := TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone);
Result :=f.Size;
F.Free;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
with OpenDialog1 do
begin
Execute;
if FileName <> '' then
begin
Edit1.Text := 'UPLOAD '+ ExtractFileName(FileName) +' '+Inttostr(GetFileSize(FileName));
Label1.Caption := FileName;
cs.Socket.SendText(edit1.Text);
end;
end;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
CS.Active := True;

end;

procedure TForm2.SendCommandClick(Sender: TObject);
var fs : TFileStream;
Buf : pointer;

begin
//CS.Socket.SendText(Edit1.Text+#13#10);
//Memo1.Lines.Add();
fs := TFileStream.Create(Label1.Caption ,fmOpenRead or fmShareDenyNone);

GetMem(Buf,fs.Size);
fs.Seek(0,soFromBeginning);

fs.ReadBuffer(Buf^,fs.Size);

memo1.Lines.Add('has send : '+inttostr(Cs.Socket.SendBuf(Buf^,fs.Size)));

end;

procedure TForm2.Button3Click(Sender: TObject);
begin
cs.Close;
end;

procedure TForm2.CSRead(Sender: TObject; Socket: TCustomWinSocket);
begin

Memo1.Lines.add(socket.receiveText);

end;

end.
 
注:上面代码来自唐晓锋 。
 
娜位我这个需求中知道硬件之间的连接方案吗?
用网线直接连接两机可以吗?需要hub吗?
多谢
 
不需要hub的,
不过连接的网线根用hub连时有点不同,到电脑城问一下就知道了。
 
两个电脑用网卡互连时,采用反接双绞线(可到电脑市场买一根,如果有工具可自己做
一根很简单)直连即可,不需要HUB
 
http://mincer.top263.net/code/downfilecode.zip
 
多人接受答案了。
 
你可以自己规定你的传送的方式,传送他的文件类型(协议),最后在生成同样后缀的文件不就可以了吗?
 
后退
顶部