如何使用TClientSocket和TServerSocket传输文件(100分)

  • 主题发起人 主题发起人 kirinma
  • 开始时间 开始时间
K

kirinma

Unregistered / Unconfirmed
GUEST, unregistred user!
如题
不用告诉我怎么传输字符串,delphi的例子里面有
 
TServerSocket用组塞模式,TClientSocket用非组塞模式。
 
好像你厉害:不用告诉我怎么传输字符串,delphi的例子里面有

那么文件的例子论坛也有啊。

 
能否说详细一点
具体到哪个属性
 
看帮助先。。。动手写测试代码。。。出了问题再来发帖子。。。比较好
 
to 张无忌
我现在不知道什么是组塞模式,什么是非组塞模式,如何写测试代码?
我使用的是流传递,
ServerSocket.Socket.Connections[0].SendStream
Socket.ReceiveBuf(F_Stream,I);
可是不行,在接收的时候出问题,CPU调试窗口都跳出来了

to tt8
论坛里有,可是我搜不到,不用你来说教

 
看来大家都放假休息了,祝大家玩的开心
 
是這樣的﹐你先將要傳送的文件先讀入到文件流中﹐再用SendBuf來傳送文件流。傳送文件時﹐最好是采用多線程的(我就是被卡在這兒了).
 
我也想知道.
 
我已經寫出來了﹐是線程Socket的﹐給你哦﹗
unit Unit1;

interface

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

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
private
{ Private declarations }
public
{ Public declarations }
end;

TClientStread=class(TThread)
protected
procedure GetFileFromStream;
procedure Execute;override;
end;

TServerStread=class(TServerClientThread)
public
procedure ClientExecute;override;
end;

var
Form1: TForm1;
FF: String;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Active:=True;
ClientSocket1.Active:=False;
end;

{ TClientStread }

procedure TClientStread.Execute;
begin
inherited;
FreeOnTerminate:= True;
Synchronize(GetFileFromStream);
end;

procedure TClientStread.GetFileFromStream;
var
Data: Array[0..1023] Of char;
S: TWinSocketStream;
F: TFileStream;
FN,A: String;
Buf: Pointer;
t: Integer;
begin
with Form1.OpenDialog1 do
begin
Execute;
if FileName<>'' then
begin
FN:=FileName;
FF:=ExtractFileName(FN);

Form1.ClientSocket1.Host:='172.17.8.205';
Form1.ClientSocket1.Active:=True;

S:=TWinSocketStream.Create(Form1.ClientSocket1.Socket,60000);
F:=TFileStream.Create(FN,fmShareDenyWrite);

try
{ if Form1.ClientSocket1.Socket.Data<>nil then
begin
A:= Form1.ClientSocket1.Socket.ReceiveText;
Showmessage(A);
end; }
T:=F.Size;
GetMem(Buf,T);
F.ReadBuffer(Buf^,T);
S.WriteBuffer(Buf^,F.Size);
// S.CopyFrom(F,0);
finally
S.Free;
F.Free;
Form1.ClientSocket1.Active:=False;
FreeMem(Buf,T);
end; //End Try

end; //End if FileName<>'' then
end; //End with Form1.OpenDialog1 do
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TClientStread.Create(False);
end;


{ TServerStread }

procedure TServerStread.ClientExecute;
var
RBuf: Array [0..4095] of char;
S: TWinSocketStream;
F: TFileStream;
BytesRead:Integer;
begin
//inherited;
While (not Terminated) and (ClientSocket.Connected) do
begin
try
FreeOnTerminate:=True;
// ClientSocket.SendText('I got');
S:= TWinSocketStream.Create(ClientSocket,60000);
F:= TFileStream.Create('C:/'+FF,fmCreate);
try
FillChar(RBuf,SizeOf(RBuf),0);
if S.WaitForData(5000) then
Repeat
BytesRead:=S.Read(RBuf,SizeOf(RBuf));
if BytesRead=0 then
ClientSocket.Close
else F.WriteBuffer(RBuf,BytesRead);
Until Not S.WaitForData(2000);
finally
F.Free;
S.Free;
ClientSocket.Close;
end; //End try finally
except
HandleException;
end; //End try except
end;

end;

procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread:= TServerStread.Create(False,ClientSocket);
end;

end.
 
感谢Jelly0228提供这么详细的代码,现在又决定不应这个东西了,不过按规定给分,毕竟学到了东西
也感谢其他人的参与
 
多人接受答案了。
 
后退
顶部