急急急!谁知道用winsocket通过代理服务器建立连接 (200分)

  • 主题发起人 主题发起人 kuangwenwei
  • 开始时间 开始时间
to duckstar
也发一份给我吧,谢谢!
goujie@msn.com
 
下程序可以传递任意大小文件,如果不符合,可以不要给我加分哟:)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;

PCON = ^TCON;

TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
ClientSocket: TClientSocket;
ServerSocket: TServerSocket;
btnServerActive: TButton;
btnClientCon: TButton;
btnClientDisConn: TButton;
BtnClientSendF: TButton;
Memo1: TMemo;
OpenDialog: TOpenDialog;
Edit1: TEdit;
Label1: TLabel;
procedure btnClientConClick(Sender: TObject);
procedure btnClientDisConnClick(Sender: TObject);
procedure BtnClientSendFClick(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure btnServerActiveClick(Sender: TObject);
procedure ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var Count : Integer;

function GetFileSize(const FileName: string):integer;
var f : TFileStream;
begin
f := TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone);
Result :=f.Size;
F.Free;
end;

procedure TForm1.btnClientConClick(Sender: TObject);
begin
ClientSocket.Active := True;
with OpenDialog do
begin
Execute;
if FileName <> '' then
begin
Edit1.Text := 'UPLOAD '+ ExtractFileName(FileName) +' '+Inttostr(GetFileSize(FileName));
Label1.Caption := FileName;
ClientSocket.Socket.SendText(edit1.Text);
end;
end;
end;

procedure TForm1.btnClientDisConnClick(Sender: TObject);
begin
ClientSocket.Active := False;
end;

procedure TForm1.BtnClientSendFClick(Sender: TObject);
var fs : TFileStream;
Buf : pointer;
begin
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(ClientSocket.Socket.SendBuf(Buf^,fs.Size)));
end;

procedure TForm1.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.add(socket.ReceiveText);
end;

procedure TForm1.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;

procedure TForm1.btnServerActiveClick(Sender: TObject);
begin
ServerSocket.Active := True;
end;

procedure TForm1.ServerSocketClientConnect(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.ServerSocketClientRead(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
Count := count + 1;
GetMem(Buffer,BufferSize);
nRetr := Socket.ReceiveBuf(Buffer^,BufferSize);
Memo1.Lines.Add(IntToStr(Count) + ' ' + IntToStr(nRetr));
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.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Count := 0;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ClientSocket.Active := False;
ServerSocket.Active := False;
end;
end.
 
至duducat
你的这段代码好象只能在局域网内传输,
在INTERNET上传输就有问题了,比如在一个局域网内的某台机器向
另外一个局域网的某台机器传输文件时,将无法联接。
如果你已经成功联接了的话,请问客户端的IP及端口要怎样设置?
 
to kuangwenwei
好的,我再看看!!!其实原理一样的
 
2个系统都在不同的局域网内,是无法用TCP建立连接,除非通过第3方代理中转!
 
to张无忌
通过第3方代理要怎样中转,有没有此类的例子
 
就是用代理服务器,这个类型的例子网上很多的,
 
能不能给我发一个,
Email:kww2002@sina.com
 
我发出去了~~~~~~~~~
 
给我发一个好吗?
rainky@hotmail.com
 
TO 张无忌
没有收到
 
我再发了一次
 
TO 张无忌
文件webserver.rar打不开
 
你用WINRAR3。0才行
 
TO 张无忌和kuangwenwei

是一个SOCKS代理吧????

能不能给我发一个代理服务器?十分感谢

cnhba2001@etang.com





 
我发了,用winrar3才开的聊[:D]
 
http://antic_ant.delphibbs.com/index4 上的p2p.exe
可以在局域网内串500m多的文件,昨天在internet上试了一下,还可以,不过速度慢点
可是我能连接别人,但是别人连不上我,因为我是在通过局域网上的internet,但是pp点点通可以
不知道他是如何实现的?
 
张兄,给我一份,我在写QQ,谢谢
 
my e-mail:
mumhello@hotmail.com
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部