一
一团火
Unregistered / Unconfirmed
GUEST, unregistred user!
Sever
=========================================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TClientThread = Class(TServerClientThread)
Private
Public
Procedure ClientExecute; override;
End;
var
Form1: TForm1;
file_name:string;
implementation
{$R *.dfm}
Procedure TClientThread.ClientExecute;
Var
ReceiveBuffer : Array[0..4095] Of Char;
SocketStream : TWinSocketStream;
BytesRead : Integer;
F : TFileStream;
Begin
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// 为什么在此接收,客户端发来的文件名,总是提示错误
//
form1.ServerSocket1.Socket.ReceiveText;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Randomize;
file_name:='c:/temp/'+'a'+inttostr(Random(10)); //暂时放了一个隋机数作为文件名
While Not Terminated And ClientSocket.Connected Do
Begin
try
SocketStream := TWinSocketStream.Create(ClientSocket, 60000);
F := TFileStream.Create(file_name, fmCreate);
try
FillChar(ReceiveBuffer, 10, 0);
If SocketStream.WaitForData(5000) Then
Repeat
BytesRead := SocketStream.Read(ReceiveBuffer,SizeOf(ReceiveBuffer));
if BytesRead = 0 then
ClientSocket.Close
Else
F.WriteBuffer(ReceiveBuffer, BytesRead);
Until Not SocketStream.WaitForData(2000);
finally
SocketStream.Free;
ClientSocket.Close;
f.Free;
end;
except
end;
End;
End;
procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TClientThread.Create(False, ClientSocket);
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('断开连接!');
memo1.Lines.Add('-------------------');
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('同意连接!');
end;
procedure TForm1.FormShow(Sender: TObject);
begin
if ServerSocket1.Active=false
then ServerSocket1.Open;
end;
end.
==============================================================================
client
=============================================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ScktComp, ComCtrls;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
BitBtn1: TBitBtn;
OpenDialog1: TOpenDialog;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
StatusBar1: TStatusBar;
Edit2: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
fsize:int64;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
s : TWinSocketStream;
f : TFileStream;
// fname:string;
begin
edit1.Text:=trim(edit1.Text);
ClientSocket1.Open;
s := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
f := TFileStream.Create(edit1.Text, fmShareDenyWrite);
//告诉远程传输文件的大小和文件名
fsize:=f.Size;
ClientSocket1.Socket.SendText(Format('%d|%s', [f.Size, ExtractFileName(OpenDialog1.FileName)]));
Try
s.CopyFrom(f, fsize);
Finally
s.Free;
f.Free;
StatusBar1.SimpleText:='传输失败!';
ClientSocket1.Close;
End;
ClientSocket1.Close;
StatusBar1.SimpleText:='传输成功!';
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
edit1.Text:=OpenDialog1.FileName;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ClientSocket1.Active
then ClientSocket1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClientSocket1.Socket.SendText('client send!');
edit1.Text:=clientsocket1.Socket.ReceiveText;
end;
end.
=========================================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TClientThread = Class(TServerClientThread)
Private
Public
Procedure ClientExecute; override;
End;
var
Form1: TForm1;
file_name:string;
implementation
{$R *.dfm}
Procedure TClientThread.ClientExecute;
Var
ReceiveBuffer : Array[0..4095] Of Char;
SocketStream : TWinSocketStream;
BytesRead : Integer;
F : TFileStream;
Begin
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// 为什么在此接收,客户端发来的文件名,总是提示错误
//
form1.ServerSocket1.Socket.ReceiveText;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Randomize;
file_name:='c:/temp/'+'a'+inttostr(Random(10)); //暂时放了一个隋机数作为文件名
While Not Terminated And ClientSocket.Connected Do
Begin
try
SocketStream := TWinSocketStream.Create(ClientSocket, 60000);
F := TFileStream.Create(file_name, fmCreate);
try
FillChar(ReceiveBuffer, 10, 0);
If SocketStream.WaitForData(5000) Then
Repeat
BytesRead := SocketStream.Read(ReceiveBuffer,SizeOf(ReceiveBuffer));
if BytesRead = 0 then
ClientSocket.Close
Else
F.WriteBuffer(ReceiveBuffer, BytesRead);
Until Not SocketStream.WaitForData(2000);
finally
SocketStream.Free;
ClientSocket.Close;
f.Free;
end;
except
end;
End;
End;
procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TClientThread.Create(False, ClientSocket);
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('断开连接!');
memo1.Lines.Add('-------------------');
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('同意连接!');
end;
procedure TForm1.FormShow(Sender: TObject);
begin
if ServerSocket1.Active=false
then ServerSocket1.Open;
end;
end.
==============================================================================
client
=============================================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ScktComp, ComCtrls;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
BitBtn1: TBitBtn;
OpenDialog1: TOpenDialog;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
StatusBar1: TStatusBar;
Edit2: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
fsize:int64;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
s : TWinSocketStream;
f : TFileStream;
// fname:string;
begin
edit1.Text:=trim(edit1.Text);
ClientSocket1.Open;
s := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
f := TFileStream.Create(edit1.Text, fmShareDenyWrite);
//告诉远程传输文件的大小和文件名
fsize:=f.Size;
ClientSocket1.Socket.SendText(Format('%d|%s', [f.Size, ExtractFileName(OpenDialog1.FileName)]));
Try
s.CopyFrom(f, fsize);
Finally
s.Free;
f.Free;
StatusBar1.SimpleText:='传输失败!';
ClientSocket1.Close;
End;
ClientSocket1.Close;
StatusBar1.SimpleText:='传输成功!';
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
edit1.Text:=OpenDialog1.FileName;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ClientSocket1.Active
then ClientSocket1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClientSocket1.Socket.SendText('client send!');
edit1.Text:=clientsocket1.Socket.ReceiveText;
end;
end.